Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Nobody
On Fri, 25 Jun 2010 12:25:56 +1200, Lawrence D'Oliveiro wrote:

> Just been reading this article
> ...
> which says that a lot of security holes are arising these days because
> everybody is concentrating on unit testing of their own particular
> components, with less attention being devoted to overall integration
> testing.
> 
> Fair enough. But it’s disconcerting to see some of the advice being
> offered in the reader comments, like “force everyone to use stored
> procedures”, or “force everyone to use prepared/parametrized
> statements”, “never construct ad-hoc SQL queries” and the like.
> 
> I construct ad-hoc queries all the time. It really isn’t that hard to
> do safely.

Wrong.

Even if you get the quoting absolutely correct (which is a very big "if"),
you have to remember to perform it every time, without exception. And you
need to perform it exactly once. As the program gets more complex,
ensuring that it's done in the correct place, and only there, gets harder.

More generally, as a program gets more complex, "this will work so long as
we do X every time without fail" approaches "this won't work".

> All you have to do is read the documentation—for example,
> —and then
> write a routine that takes arbitrary data and turns it into a valid
> string literal, like this
> .

That's okay. Provided the documentation is accurate. And provided that you
update the escaping algorithm whenever the SQL dialect gets extended, or
you switch to a different back-end, or modify the program. IOW, it's not
even remotely okay.

"Unparsing" data so that you get the correct answer out of a subsequent
parsing step is objectively and obviously the wrong approach. The
correct approach is to skip both the unparsing and parsing steps
entirely.

Formal grammars are a useful way to represent graph-like data structures
in a human-readable and human-editable form. But for creation,
modification and use by a computer, it is invariably preferable to operate
upon the graph directly. Textual formats inherit all of the "issues" which
apply to the underlying data structure, then add a few of their own for
good measure.

> I've done this sort of thing for MySQL, for HTML and JavaScript (in both
> Python and JavaScript itself), and for Bash.

And, of course, you're convinced that you got it right every time. That
attitude alone should set alarm bells ringing for anyone who's worked in
this industry for more than five minutes.

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


Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Cameron Simpson
On 25Jun2010 15:54, I wrote:
| The number of times I've had to
| fix/remove insert-values-into-SQL-text code ...

My point here is that with insert-escaped-values-into-sql-text,
you only need to forget to do it once (or do it wrong).
By using a parameterised form like that required by SQLalchemy
the library does it and never forgets.

I would also point out that if you use a library to _construct_ the SQL
statements themselves eg via SQLA's .select() methods etc then you will never
introduce a syntax error into the SQL either. I expect I could construct SQL
syntax errors that cause havoc when inserted with correctly escaped parameter
values if I tried, probably using quotes in the SQL typo part.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

George, discussing a patent and prior art:
"Look, this  publication has a date, the patent has a priority date,
can't you just compare them?"
Paul Sutcliffe:
"Not unless you're a lawyer."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Cameron Simpson
On 25Jun2010 15:38, Lawrence D'Oliveiro  
wrote:
| In message <2010062422432660794-angrybald...@gmailcom>, Owen Jacobson wrote:
| > Why would I write this when SQLAlchemy, even without using its ORM
| > features, can do it for me?
| 
| SQLAlchemy doesn’t seem very flexible. Looking at the code examples 
| , they’re very procedural: 
| build object, then do a string of separate method calls to add data to it. I 
| prefer the functional approach, as in my table-update example.

He said "without using its ORM". I do what you suggest (make SQL
statements at need) using SQLalchemy all the time. It is simple and easy
and _robust_ against odd data. The number of times I've had to
fix/remove insert-values-into-SQL-text code ...
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Plague, Famine, Pestilence, and C++ stalk the land. We're doomed! Doomed!
- Simon E Spero
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] pyparsing 1.5.3 released

2010-06-24 Thread Paul McGuire
I'm happy to announce that a new release of pyparsing is now
available,
version 1.5.3.  It has been almost a year and a half since 1.5.2 was
released, but pyparsing has remained pretty stable.

I believe I have cleaned up the botch-job I made in version 1.5.2 of
trying to support both Python 2.x and Python 3.x.  This new release
will handle it by:
- providing version-specific binary installers for Windows users
- use version-adaptive code in the source distribution to use the
  correct version of pyparsing.py for the current Python distribution

This release also includes a number of small bug-fixes, plus some
very
interesting new examples.


Here is the high-level summary of what's new in pyparsing 1.5.3:

- === NOTE:  API CHANGE!!! ===
  With this release, and henceforward, the pyparsing module is
  imported as "pyparsing" on both Python 2.x and Python 3.x versions.

- Fixed up setup.py to auto-detect Python version and install the
  correct version of pyparsing - suggested by Alex Martelli,
  thanks, Alex! (and my apologies to all those who struggled with
  those spurious installation errors caused by my earlier
  fumblings!)

- Fixed bug on Python3 when using parseFile, getting bytes instead of
  a str from the input file.

- Fixed subtle bug in originalTextFor, if followed by
  significant whitespace (like a newline) - discovered by
  Francis Vidal, thanks!

- Fixed very sneaky bug in Each, in which Optional elements were
  not completely recognized as optional - found by Tal Weiss, thanks
  for your patience.

- Fixed off-by-1 bug in line() method when the first line of the
  input text was an empty line. Thanks to John Krukoff for submitting
  a patch!

- Fixed bug in transformString if grammar contains Group expressions,
  thanks to patch submitted by barnabas79, nice work!

- Fixed bug in originalTextFor in which trailing comments or
otherwised
  ignored text got slurped in with the matched expression.  Thanks to
  michael_ramirez44 on the pyparsing wiki for reporting this just in
  time to get into this release!

- Added better support for summing ParseResults, see the new example,
  parseResultsSumExample.py.

- Added support for composing a Regex using a compiled RE object;
  thanks to my new colleague, Mike Thornton!

- In version 1.5.2, I changed the way exceptions are raised in order
  to simplify the stacktraces reported during parsing.  An anonymous
  user posted a bug report on SF that this behavior makes it difficult
  to debug some complex parsers, or parsers nested within parsers. In
  this release I've added a class attribute
ParserElement.verbose_stacktrace,
  with a default value of False. If you set this to True, pyparsing
will
  report stacktraces using the pre-1.5.2 behavior.

- Some interesting new examples, including a number of parsers related
  to parsing C source code:

  . pymicko.py, a MicroC compiler submitted by Zarko Zivanov.
(Note: this example is separately licensed under the GPLv3,
and requires Python 2.6 or higher.)  Thank you, Zarko!

  . oc.py, a subset C parser, using the BNF from the 1996 Obfuscated C
Contest.

  . select_parser.py, a parser for reading SQLite SELECT statements,
as specified at http://www.sqlite.org/lang_select.html; this goes
into much more detail than the simple SQL parser included in
pyparsing's
source code

  . stateMachine2.py, a modified version of stateMachine.py submitted
by Matt Anderson, that is compatible with Python versions 2.7 and
above - thanks so much, Matt!

  . excelExpr.py, a *simplistic* first-cut at a parser for Excel
expressions, which I originally posted on comp.lang.python in
January,
2010; beware, this parser omits many common Excel cases (addition
of
numbers represented as strings, references to named ranges)

  . cpp_enum_parser.py, a nice little parser posted my Mark Tolonen on
comp.lang.python in August, 2009 (redistributed here with Mark's
permission).  Thanks a bunch, Mark!

  . partial_gene_match.py, a sample I posted to Stackoverflow.com,
implementing a special variation on Literal that does "close"
matching,
up to a given number of allowed mismatches.  The application was
to
find matching gene sequences, with allowance for one or two
mismatches.

  . tagCapture.py, a sample showing how to use a Forward placeholder
to
enforce matching of text parsed in a previous expression.

  . matchPreviousDemo.py, simple demo showing how the
matchPreviousLiteral
helper method is used to match a previously parsed token.


Download pyparsing 1.5.3 at http://sourceforge.net/projects/pyparsing/.
You
can also access pyparsing's epydoc documentation online at
http://packages.python.org/pyparsing/.

The pyparsing Wiki is at http://pyparsing.wikispaces.com.

-- Paul


Pyparsing is a pure-Python class library for quickly developing
recursive-descent parsers.  Parser grammars are assembled directly in
the 

Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Lawrence D'Oliveiro
In message <2010062422432660794-angrybald...@gmailcom>, Owen Jacobson wrote:

> Why would I write this when SQLAlchemy, even without using its ORM
> features, can do it for me?

SQLAlchemy doesn’t seem very flexible. Looking at the code examples 
, they’re very procedural: 
build object, then do a string of separate method calls to add data to it. I 
prefer the functional approach, as in my table-update example.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Lawrence D'Oliveiro
In message , Roy Smith wrote:

> 1) Somebody is running your application (or the database server) with
> the locale set to something unexpected.

Locales are under program control, so that won’t happen.

This is why I use UTF-8 encoding for everything.
-- 
http://mail.python.org/mailman/listinfo/python-list


improving python performance by extension module (64bit)

2010-06-24 Thread Peng Yu
http://psyco.sourceforge.net/

The above package can improve python program on 32 bit library. But I
need to run on 64 bit library. Is there any other module that can help
improving the performance of python on 64 bit?

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


Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Owen Jacobson

On 2010-06-24 21:02:48 -0400, Roy Smith said:


In article ,
 Lawrence D'Oliveiro  wrote:


I construct ad-hoc queries all the time. It really isn’t that hard to do
safely. All you have to do is read the documentation


I get worried when people talk about how easy it is to do something
safely.


First: I agree with this. While it's definitely possible to correctly 
escape a given SQL dialect under controlled conditions, it's not at all 
easy to get it right, and the real world is even more unfriendly than 
most people expect. Furthermore there's no reason to do it that way: 
Python's DB API spec effectively requires that placeholder parameters 
of *some* kind exist. Even if you feel the need to construct SQL, you 
can construct it with parameters almost as easily as you can construct 
it with the values baked in.


With that said...


2) Somebody runs your application with a different PYTHONPATH, which
causes a different (i.e. malicious) urllib module to get loaded, which
makes urllib.quote() do something you didn't expect.


Someone who can manipulate PYTHONPATH or otherwise add code to the 
runtime environment is already in a position to hose your database, 
independently of escaping-related issues. It's up to the sysadmin or 
user to ensure that their environment is sane, and it's on their head 
if they add broken code to a program's runtime environment.


Lawrence D'Oliveiro wrote:


I'€™ve done this sort of thing for MySQL, for HTML and JavaScript (in both
Python and JavaScript itself), and for Bash. It’s not hard to verify you’ve
done it correctly. It lets you easily create table-updating code like the
following, which makes it so easy to update the code to track changes in the
database structure:

 sql.cursor.execute \
  (
"update items set "
+
", ".join
(
tuple
(
"%(name)s = %(value)s"
%
{
"name" : field[0],
"value" : SQLString(Params.getvalue
  (
"%s[%s]" % (field[1], 
urllib.quote(modify_id))

  ))
}
for field in
(
("class_name", "modify_class"),
("make", "modify_make"),
("model", "modify_model"),
("details", "modify_details"),
("serial_nr", "modify_serial"),
("inventory_nr", "modify_invent"),
("when_purchased", "modify_when_purchased"),
... you get the idea ...
("location_name", "modify_location"),
("comment", "modify_comment"),
)
)
+
(
"last_modified = %d" % int(time.time()),
)
)
+
" where inventory_nr = %s" % SQLString(modify_id)
  )


Why would I write this when SQLAlchemy, even without using its ORM 
features, can do it for me? It even uses the placeholder-generating 
strategy I mentioned above, where possible.


Finally, it's worth noting that MySQL is (almost) the only mainstream 
database that uses escaping for parameterization. PostgreSQL, SQL 
Server, Oracle, DB2, and most other databases support parameters 
natively in their communication protocols: parameters aren't injected 
into the query string, but are sent separately and processed separately 
within the DBMS. This neatly avoids encoding-related and 
quoting-related problems entirely, and it means the type of the 
parameter can be preserved if it's useful.


-o

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


Re: Heuristic

2010-06-24 Thread Marcos
On 25 jun, 03:36, geremy condra  wrote:
> On Thu, Jun 24, 2010 at 9:13 PM, Marcos  wrote:
> > I have a store, so I want to maximize the profit. I have all the
> > suppliers with diferent prices, some providers can send products to a
> > client an others not, this has a plus price. Some providers has a
> > discount over the tansport if a quantity is reached.
>
> > Sometimes its better to me receive the order and resend to the client
> > if I have a transport discount.
>
> > Not all the suppliers has all products for a order.
>
> > So I want to create a function which I can pass the data, and
> > generates all the possibilities so I can find the maximum profit.
>
> > Have I to use heuristics? Do you know some examples?.
>
> > Thanks in advance.
>
> Depending on the exact constraints this may be a linear programming
> problem (very simple) and even if it isn't there are likely to be good
> approximation methods. With more information will come better
> advice.
>
> Geremy Condra

I geremy Im goint to put you some example:

Providers:

Number 1- Product 1: 56$ Product 4: 45$
Number 2- Product 2: 24$ Product 4: 34$
Number 3- Product 1: 45$ Product 2: 65$ Product 3: 65$ Product 4: 76$

Transport
Provider number 3 can transport direct to customer with a plus of 5$,
It can deply to a distribution center.
Provider number 2 and 1 only transport to a distribution center.
Poriver number 2 gives free transport over 500$, and number 1 over
1000$.

Each provider has a transport cost, that we have in tables.

If we deply to the distribution center we have to add the transport to
the consumer.

Order:

10 units product 1
6 units product 2
20 units product 3
40 units product 4


So which is the best way to deploy the order optimizing profit?.

This example has random quantities of product and prices, but I think
now its clear.

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


Re: Heuristic

2010-06-24 Thread Marcos
On 25 jun, 03:28, Terry Reedy  wrote:
> On 6/24/2010 9:13 PM, Marcos wrote:
>
> > I have a store, so I want to maximize the profit. I have all the
> > suppliers with diferent prices, some providers can send products to a
> > client an others not, this has a plus price. Some providers has a
> > discount over the tansport if a quantity is reached.
>
> > Sometimes its better to me receive the order and resend to the client
> > if I have a transport discount.
>
> > Not all the suppliers has all products for a order.
>
> > So I want to create a function which I can pass the data, and
> > generates all the possibilities so I can find the maximum profit.
>
> > Have I to use heuristics? Do you know some examples?.
>
> You would not use a heuristic to generate all possibilities. You might
> use one to *avoid* doing that, and still get a good, not necessarily
> optimal, answer. Wikipedia has an general entry on 'heuristic'.
> Algorithm books often specifically discuss heuristic algorithms.
>
> --
> Terry Jan Reedy

Thanks terry I am reading about Knapsack problem, but the main problem
I have its the repeated elements, so If i am itering all elements some
can be repeated, I dont know how to avoid this, I thinked in neural
networks too.

Do you have any ideas?.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Heuristic

2010-06-24 Thread geremy condra
On Thu, Jun 24, 2010 at 9:13 PM, Marcos  wrote:
> I have a store, so I want to maximize the profit. I have all the
> suppliers with diferent prices, some providers can send products to a
> client an others not, this has a plus price. Some providers has a
> discount over the tansport if a quantity is reached.
>
> Sometimes its better to me receive the order and resend to the client
> if I have a transport discount.
>
> Not all the suppliers has all products for a order.
>
> So I want to create a function which I can pass the data, and
> generates all the possibilities so I can find the maximum profit.
>
> Have I to use heuristics? Do you know some examples?.
>
> Thanks in advance.

Depending on the exact constraints this may be a linear programming
problem (very simple) and even if it isn't there are likely to be good
approximation methods. With more information will come better
advice.

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


Re: deprecated string module issue

2010-06-24 Thread Benjamin Peterson
GrayShark  gmail.com> writes:

> Sorry, I meant "from string import lowercase, uppercase"

Technically, you should use ascii_lowercase and ascii_uppercase, though I don't
know if that's the cause of pylint's complaints.



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


Re: Heuristic

2010-06-24 Thread Terry Reedy

On 6/24/2010 9:13 PM, Marcos wrote:

I have a store, so I want to maximize the profit. I have all the
suppliers with diferent prices, some providers can send products to a
client an others not, this has a plus price. Some providers has a
discount over the tansport if a quantity is reached.

Sometimes its better to me receive the order and resend to the client
if I have a transport discount.

Not all the suppliers has all products for a order.

So I want to create a function which I can pass the data, and
generates all the possibilities so I can find the maximum profit.

Have I to use heuristics? Do you know some examples?.


You would not use a heuristic to generate all possibilities. You might 
use one to *avoid* doing that, and still get a good, not necessarily 
optimal, answer. Wikipedia has an general entry on 'heuristic'. 
Algorithm books often specifically discuss heuristic algorithms.


--
Terry Jan Reedy

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


Heuristic

2010-06-24 Thread Marcos
I have a store, so I want to maximize the profit. I have all the
suppliers with diferent prices, some providers can send products to a
client an others not, this has a plus price. Some providers has a
discount over the tansport if a quantity is reached.

Sometimes its better to me receive the order and resend to the client
if I have a transport discount.

Not all the suppliers has all products for a order.

So I want to create a function which I can pass the data, and
generates all the possibilities so I can find the maximum profit.

Have I to use heuristics? Do you know some examples?.

Thanks in advance.

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


Re: Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Roy Smith
In article ,
 Lawrence D'Oliveiro  wrote:

> I construct ad-hoc queries all the time. It really isn’t that hard to do
> safely. All you have to do is read the documentation

I get worried when people talk about how easy it is to do something 
safely.  Let me suggest a couple of things you might not have considered:

1) Somebody is running your application (or the database server) with 
the locale set to something unexpected.  This might change how numbers, 
dates, currency, etc, get formatted, which could change the meaning of 
your constructed SQL statement.

2) Somebody runs your application with a different PYTHONPATH, which 
causes a different (i.e. malicious) urllib module to get loaded, which 
makes urllib.quote() do something you didn't expect.
 
> I’ve done this sort of thing for MySQL, for HTML and JavaScript (in both
> Python and JavaScript itself), and for Bash. It’s not hard to verify 
> you’ve
> done it correctly. It lets you easily create table-updating code like the
> following, which makes it so easy to update the code to track changes in the
> database structure:
> 
>  sql.cursor.execute \
>   (
> "update items set "
> +
> ", ".join
> (
> tuple
> (
> "%(name)s = %(value)s"
> %
> {
> "name" : field[0],
> "value" : SQLString(Params.getvalue
>   (
> "%s[%s]" % (field[1], 
> urllib.quote(modify_id))
>   ))
> }
> for field in
> (
> ("class_name", "modify_class"),
> ("make", "modify_make"),
> ("model", "modify_model"),
> ("details", "modify_details"),
> ("serial_nr", "modify_serial"),
> ("inventory_nr", "modify_invent"),
> ("when_purchased", "modify_when_purchased"),
> ... you get the idea ...
> ("location_name", "modify_location"),
> ("comment", "modify_comment"),
> )
> )
> +
> (
> "last_modified = %d" % int(time.time()),
> )
> )
> +
> " where inventory_nr = %s" % SQLString(modify_id)
>   )
-- 
http://mail.python.org/mailman/listinfo/python-list


Why Is Escaping Data Considered So Magical?

2010-06-24 Thread Lawrence D'Oliveiro
Just been reading this article

which says that a lot of security holes are arising these days because
everybody is concentrating on unit testing of their own particular
components, with less attention being devoted to overall integration
testing.

Fair enough. But it’s disconcerting to see some of the advice being offered
in the reader comments, like “force everyone to use stored procedures”, or
“force everyone to use prepared/parametrized statements”, “never construct
ad-hoc SQL queries” and the like.

I construct ad-hoc queries all the time. It really isn’t that hard to do
safely. All you have to do is read the documentation—for example,
—and then write a
routine that takes arbitrary data and turns it into a valid string literal,
like this .

I’ve done this sort of thing for MySQL, for HTML and JavaScript (in both
Python and JavaScript itself), and for Bash. It’s not hard to verify you’ve
done it correctly. It lets you easily create table-updating code like the
following, which makes it so easy to update the code to track changes in the
database structure:

 sql.cursor.execute \
  (
"update items set "
+
", ".join
(
tuple
(
"%(name)s = %(value)s"
%
{
"name" : field[0],
"value" : SQLString(Params.getvalue
  (
"%s[%s]" % (field[1], 
urllib.quote(modify_id))
  ))
}
for field in
(
("class_name", "modify_class"),
("make", "modify_make"),
("model", "modify_model"),
("details", "modify_details"),
("serial_nr", "modify_serial"),
("inventory_nr", "modify_invent"),
("when_purchased", "modify_when_purchased"),
... you get the idea ...
("location_name", "modify_location"),
("comment", "modify_comment"),
)
)
+
(
"last_modified = %d" % int(time.time()),
)
)
+
" where inventory_nr = %s" % SQLString(modify_id)
  )

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


Re: Types missing from "types"module

2010-06-24 Thread Terry Reedy

On 6/24/2010 2:29 AM, John Nagle wrote:


Does the "types" module go away in 3.x, then?


No, but all the duplication (IntType is int, etc) is removed.

>>> dir(types)
['BuiltinFunctionType', 'BuiltinMethodType', 'CodeType', 'FrameType', 
'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'LambdaType', 
'MemberDescriptorType', 'MethodType', 'ModuleType', 'TracebackType', 
'__builtins__', '__doc__', '__file__', '__name__', '__package__']

--
Terry Jan Reedy

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


Re: How to send a non-standard IMAP command?

2010-06-24 Thread Xianwen Chen
On Thu, Jun 24, 2010 at 2:25 PM, Tim Chase wrote:

> On 06/24/2010 04:47 AM, Xianwen Chen wrote:
>
>> Thanks a lot for your reply! I thought it would be simpler if the
>> problem was presented in a brief way. Unfortunately, not for this
>> case.
>>
>> Here is the detail. Free Yahoo! mail accounts can be accsessed via
>> IMAP protocal, however, a non-standard shake hand code is needed
>> before log in [1]:
>>
>> ID ("GUID" "1")
>>
>> . This is what I'm now working for. I tried:
>>
>> IMAP4.xatom('','ID ("GUID" "1")','',)
>>
>> and
>>
>> dest_srv.xatom('ID ("GUID" "1")')
>>
>> , but I got error messages. Any hint please?
>>
>
> In general, it would be helpful to include the error-message(s) you get.
>  However, I tried it with a junk Yahoo account I set up:
>
>  from imaplib import IMAP4
>  i = IMAP4("imap.mail.yahoo.com")
>  USER = 'yourusern...@yahoo.com'
>  PASS = 'your secret goes here'
>  # per the Wikipedia page you gave
>  # the ID has to happen before login
>  i.xatom('ID ("GUID" "1")')
>
>  i.login(USER, PASS)
>  i.select()
>  typ, data = i.search(None, 'ALL')
>  for num in data[0].split():
>typ, data = i.fetch(num, '(RFC822)')
>message = data[0][1].splitlines()
>subject = [line
>  for line in message
>  if line.lower().startswith('subject: ')
>  ][0]
>print num, subject
>  i.close()
>  i.logout()
>
> and it worked.
>
> -tkc
>
>
>
>

Hi Tim,

The problem was the password. I was careless. Thanks for your advice. Next
time I'll have error codes posted.

And thanks a lot for your constructive example!

I have a strange problem that

"M = imaplib.IMAP4_SSL(M_addr)
M.debug = 2"

doesn't work. No verbose output at all. Any hint please?

Best regards,

Xianwen


-- 
Xianwen Chen
University of Tromsø
http://u.nu/9uubc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mod_python and css

2010-06-24 Thread John Nagle

   If you send back a content type of "text/plain", your output
will not be rendered as HTML by a browser; it will just be
displayed as plain text.

   Try "text/html".

John Nagle

On 6/24/2010 12:19 PM, Thomas Jollans wrote:

On 06/24/2010 09:10 PM, Bradley Hintze wrote:

Hi all,


about your question, no idea.



the mod_python mailing list is pretty dead so I thought I'd try asking
my question here.


It comes as no surprise to me that the mod_python mailing list is dead:
nobody uses mod_python anymore. For all I know, it might be broken, and
the API was (IIRC) never particularly nice.

IMHO, you should use WSGI instead: it's an interface portable across web
server boundaries, and, with mod_wsgi, it's just as high-performance on
apache as was mod_python.



I am using mod_python and am having trouble with my .py scripts that
output HTML. the HTML comes out alright but my style sheets are not
being rendered. I am sure that this has to do withe a mod handler
problem.


What does "not being rendered" mean? Do you have an exception traceback?
Can you maybe a full transcript of HTTP headers and output? (by
telnet'ing to the web server perhaps)



Here is what I have in httpd.conf:


 AddHandler mod_python .css
 PythonHandler handler::handler_css
 PythonDebug On



Here is what handler.py looks like:

from mod_python import apache

def _dump(req, extension):
 req.content_type = 'text/plain'
 print>>  req, 'uri = %s' % req.uri
 print>>  req, 'filename = %s' % req.filename
 print>>  req, 'path_info = %s' % req.path_info
 print>>  req, 'extension = %s' % extension
 return apache.OK

def handler(req):
 return _dump(req, '')

def handler_css(req):
 return _dump(req, '.css')


Any ideas on what I'm doing wrong?





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


Re: Should I Learn Python or Ruby next?

2010-06-24 Thread Rhodri James
On Wed, 23 Jun 2010 23:47:55 +0100, rantingrick   
wrote:



On Jun 23, 4:43 pm, "Rhodri James" 
wrote:


> And how exactly does your example express itself in a more
> "syntactically-correct" "linear-flow" than the two code snippets i
> provided earlier, hmmm?

You did rather carefully pick an example where Python's syntax flow the  
 other way round


"rather carefully picked" you say? As if built-in functions are hardly
ever used? No I think *your* statement was "rather carefully picked"
to try and discredit me. Sorry my friend that might work on the less
astute readers round here, but it has no effect on me ;-)


One word: "map".

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: question about multiprocessing

2010-06-24 Thread News123
hywhy wrote:
> thanks,friend!
> I wanna use Queue to share objects,but on windows, the multiprocessing
> module can‘t do this。 Is there any way to solve this problem!
> thanks



With  multiprocessing.SyncManager you should be able to exchange Queues
between different processes.

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


Re: GDAL-1.7.1 : vcvarsall.bat missing

2010-06-24 Thread Christian Heimes
>  I am attempting to install the GDAL bindings (GDAL-1.7.1) on a
> Windows XP Desktop with Python 2.6 and GDAL. During install, the
> script finishes with "error: Unable to find vcvarsall.bat".
> 
> What dependencies am I missing?

Visual Studio 2008

Christian

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


GDAL-1.7.1 : vcvarsall.bat missing

2010-06-24 Thread kBob

 I am attempting to install the GDAL bindings (GDAL-1.7.1) on a
Windows XP Desktop with Python 2.6 and GDAL. During install, the
script finishes with "error: Unable to find vcvarsall.bat".

What dependencies am I missing?


C:\PyPI\GDAL-1.7.1>c:\python26\python setup.py install
running install
running bdist_egg
running egg_info
writing GDAL.egg-info\PKG-INFO
writing top-level names to GDAL.egg-info\top_level.txt
writing dependency_links to GDAL.egg-info\dependency_links.txt
reading manifest file 'GDAL.egg-info\SOURCES.txt'
writing manifest file 'GDAL.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
running build_ext
building 'osgeo._gdal' extension
error: Unable to find vcvarsall.bat

Kelly Dean
Fort Collins, CO
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mod_python and css

2010-06-24 Thread Bradley Hintze
I had some issues with WSGI install. It was duious at best as it
failed and seemed very unstable. Is this a trusted application?

On Thu, Jun 24, 2010 at 3:19 PM, Thomas Jollans  wrote:
> On 06/24/2010 09:10 PM, Bradley Hintze wrote:
>> Hi all,
>
> about your question, no idea.
>
>>
>> the mod_python mailing list is pretty dead so I thought I'd try asking
>> my question here.
>
> It comes as no surprise to me that the mod_python mailing list is dead:
> nobody uses mod_python anymore. For all I know, it might be broken, and
> the API was (IIRC) never particularly nice.
>
> IMHO, you should use WSGI instead: it's an interface portable across web
> server boundaries, and, with mod_wsgi, it's just as high-performance on
> apache as was mod_python.
>
>>
>> I am using mod_python and am having trouble with my .py scripts that
>> output HTML. the HTML comes out alright but my style sheets are not
>> being rendered. I am sure that this has to do withe a mod handler
>> problem.
>
> What does "not being rendered" mean? Do you have an exception traceback?
> Can you maybe a full transcript of HTTP headers and output? (by
> telnet'ing to the web server perhaps)
>
>>
>> Here is what I have in httpd.conf:
>>
>> 
>>     AddHandler mod_python .css
>>     PythonHandler handler::handler_css
>>     PythonDebug On
>> 
>>
>>
>> Here is what handler.py looks like:
>>
>> from mod_python import apache
>>
>> def _dump(req, extension):
>>     req.content_type = 'text/plain'
>>     print >> req, 'uri = %s' % req.uri
>>     print >> req, 'filename = %s' % req.filename
>>     print >> req, 'path_info = %s' % req.path_info
>>     print >> req, 'extension = %s' % extension
>>     return apache.OK
>>
>> def handler(req):
>>     return _dump(req, '')
>>
>> def handler_css(req):
>>     return _dump(req, '.css')
>>
>>
>> Any ideas on what I'm doing wrong?
>>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine
801-712-8799
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: deprecated string module issue

2010-06-24 Thread Terry Reedy

On 6/24/2010 1:50 PM, Ixokai wrote:

On Thu, Jun 24, 2010 at 10:39 AM, GrayShark 
 So if the constants are not deprecated, why is the module? Is some



Again: the string module is not deprecated. It simply is not.


GrayShark, pylint has a bug. Tell the author that its over-enthusiastic 
message mislead you.

--
Terry Jan Reedy

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


Please book now for EuroPython 2010: 17th to 24th July in Birmingham, UK

2010-06-24 Thread Zeth
EuroPython 2010
---

EuroPython is a conference for the Python programming language
community, including software written in Python such as Django, Zope
and so on. It is aimed at everyone from beginners through to core
developers. This year EuroPython will be held from the 17th to 24th
July in Birmingham, UK. The 18th and 19th are tutorial days, with the
main conference kicking off from the 20th. EuroPython will include
over 100 talks, tutorials, sprints and social events. See the
timetable here:

http://www.europython.eu/talks/timetable/

Registration


Registration is open now at: http://www.europython.eu/registration/

EuroPython 2010 is a not-for-profit volunteer-organised conference.
There is a fee for your conference booking in order to help cover
costs; this includes lunches and refreshments, as well as the
conference dinner. A concessionary rate applies to: over 60's,
full-time students, unwaged, nurses, speakers and crew volunteers.

The registration page ( http://www.europython.eu/registration/ ),
explains in further detail.

Spread the Word
---

We are a community-run not-for-profit conference. Please help to
spread the word by distributing this announcement to colleagues,
project mailing lists, friends, your blog, Web site, and through your
social networking connections. Take a look at our publicity resources:

http://wiki.europython.eu/Publicity

Sponsors


Sponsoring EuroPython is a unique opportunity to affiliate with this
prestigious conference and to reach a large number of Python users
from computing professionals to academics, from entrepreneurs to
motivated and well-educated job seekers. See the following page for
more information:

http://www.europython.eu/sponsors/

General Information
---

For more information about the conference, please visit the official
site: http://www.europython.eu/

Looking forward to see you!

The EuroPython Team
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mod_python and css

2010-06-24 Thread Thomas Jollans
On 06/24/2010 09:10 PM, Bradley Hintze wrote:
> Hi all,

about your question, no idea.

> 
> the mod_python mailing list is pretty dead so I thought I'd try asking
> my question here.

It comes as no surprise to me that the mod_python mailing list is dead:
nobody uses mod_python anymore. For all I know, it might be broken, and
the API was (IIRC) never particularly nice.

IMHO, you should use WSGI instead: it's an interface portable across web
server boundaries, and, with mod_wsgi, it's just as high-performance on
apache as was mod_python.

> 
> I am using mod_python and am having trouble with my .py scripts that
> output HTML. the HTML comes out alright but my style sheets are not
> being rendered. I am sure that this has to do withe a mod handler
> problem.

What does "not being rendered" mean? Do you have an exception traceback?
Can you maybe a full transcript of HTTP headers and output? (by
telnet'ing to the web server perhaps)

> 
> Here is what I have in httpd.conf:
> 
> 
> AddHandler mod_python .css
> PythonHandler handler::handler_css
> PythonDebug On
> 
> 
> 
> Here is what handler.py looks like:
> 
> from mod_python import apache
> 
> def _dump(req, extension):
> req.content_type = 'text/plain'
> print >> req, 'uri = %s' % req.uri
> print >> req, 'filename = %s' % req.filename
> print >> req, 'path_info = %s' % req.path_info
> print >> req, 'extension = %s' % extension
> return apache.OK
> 
> def handler(req):
> return _dump(req, '')
> 
> def handler_css(req):
> return _dump(req, '.css')
> 
> 
> Any ideas on what I'm doing wrong?
> 

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


mod_python and css

2010-06-24 Thread Bradley Hintze
Hi all,

the mod_python mailing list is pretty dead so I thought I'd try asking
my question here.

I am using mod_python and am having trouble with my .py scripts that
output HTML. the HTML comes out alright but my style sheets are not
being rendered. I am sure that this has to do withe a mod handler
problem.

Here is what I have in httpd.conf:


AddHandler mod_python .css
PythonHandler handler::handler_css
PythonDebug On



Here is what handler.py looks like:

from mod_python import apache

def _dump(req, extension):
req.content_type = 'text/plain'
print >> req, 'uri = %s' % req.uri
print >> req, 'filename = %s' % req.filename
print >> req, 'path_info = %s' % req.path_info
print >> req, 'extension = %s' % extension
return apache.OK

def handler(req):
return _dump(req, '')

def handler_css(req):
return _dump(req, '.css')


Any ideas on what I'm doing wrong?

-- 
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine
801-712-8799
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Second attempt WAS: From Dict to Classes yes or no and how

2010-06-24 Thread Colin J. Williams

On 22-Jun-10 08:03 AM, Jerry Rocteur wrote:

On Tue, Jun 22, 2010 at 9:32 PM, Jerry Rocteur  wrote:
If you were able to ask us perhaps a more specific question
and describe your problem a little more concisely perhaps
I (and we) might have a bit more to offer you.


I have a dictionary:

users[key] = {'user': key,
   'secnum'  : secnum,
   'name': name
  }

Is it possible for me to code a class like this:

class GRPUser(object):
 def __init__(self, user, secnum, name, groups=None):
 self.user  = user
 self.secnum= secnum
 self.name  = name

Which would allow me to iterate through and access specific records ?

How do I iterate through and access an individual user record!

Thanks in advance,

Jerry


Jerry,

You are pushing your luck with so many repetitions of your posting.

First, you probably don't need the line 'user':key.  items() delivers 
this for you.


If every record has the same structure, you could work with the tuple 
(secnum, name).


It's not clear what the role of the class instance will be.

Colin W.

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


Re: Emacs Form Feed (^L) Display Suggestion and Tips

2010-06-24 Thread Thomas Jollans
On 06/24/2010 03:14 PM, Xah Lee wrote:
> On Jun 24, 5:41 am, Xah Lee  wrote:
>> • Emacs Form Feed (^L) Display Suggestion and Tips
>> http://xahlee.org/emacs/modernization_formfeed.html
> 
> a follow up question.
> 
> when i was learning python in ~2005, i remember seeing it in python
> code, but i haven't done much python since. Does python source code
> still have that now and then? Is there some official guide on marking
> sections?
> 
> also, besides emacs elisp, does anyone see the form feed char in other
> lang source code?

The GNU source code is full of them, I think.

> 
> thanks.
> 
>   Xah
> ∑ http://xahlee.org/
> 
> ☄

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


Re: Emacs Form Feed (^L) Display Suggestion and Tips

2010-06-24 Thread Tassilo Horn
Xah Lee  writes:

Hi Xah,

> also, besides emacs elisp, does anyone see the form feed char in other
> lang source code?

It's quite often used in messages in newsgroups and mailing lists.  The
Gnus news- and mailreader creates nice "Next/Previous page" buttons from
them and hides the next/previous part.  Really good for posting fun
questions, so that you don't see the answer directly.

Why did the chicken cross the road?



To show the raccoon it could be done!!

Bye,
Tassilo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print line number and source filename

2010-06-24 Thread Vinay Sajip
On Jun 24, 4:07 am, Peng Yu  wrote:
> %(module)s only print to "__init__". However, I need the fullname
> a.b.__init__. I looked at the manual, but I don't see what format
> string I should supply. Would you please let me know?
>

Did you look at this part of the documentation?

http://docs.python.org/library/logging.html#formatter-objects

You should use in each of your modules

logger = logging.getLogger(__name__)

and in the format string, use %(name)s. This will give "a.b" for a
logger defined in /a/b/__init__.py, "a" for a logger
defined in /a/__init__.py, "a.b.c" for a logger defined in
/a/b/c.py, and so on.

Regards,

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


Re: modifying standard library functionality (difflib)

2010-06-24 Thread Paul Rubin
Bruno Desthuilliers  writes:
> "patching source code" canonically means "physically" modifying the
> original source file. Monkeypatching - which can only be done in some
> dynamic languages - is what you're doing above, ie dynamically
> replacing a given feature at runtime.

I came across a less polite term for this, analogous with "duck typing":

 http://justatheory.com/computers/programming/methodology/fuck-typing.html

Example application in perl:

 http://www.justatheory.com/computers/programming/perl/fuck-typing-lwp.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: deprecated string module issue

2010-06-24 Thread Thomas Jollans
On 06/24/2010 07:39 PM, GrayShark wrote:
> On Jun 24, 10:06 am, GrayShark  wrote:
>> In my code I have:
>> from string import lower, upper
>>
>> When I use pylint on the program I get just one warning:
>>
>> Uses of a deprecated module 'string'.
>>
>> Iv'e noted that many if not all string functions are now in _builtin_.
>> Where are the constants?
>>
>> Thanks
>> Steven
> 
> Sorry, I meant "from string import lowercase, uppercase"
> 
> As I was joining these two, I just changed the import to 'letters'
> 
> So if the constants are not deprecated, why is the module? Is some
> other
> state then deprecated needed to describe functions and/or methods
> deprecated,
> but not whole modules?

It's not.

http://docs.python.org/library/string.html

"""The string module contains a number of useful constants and classes,
as well as some deprecated legacy functions that are also available as
methods on strings."""

Maybe it's deprecated in pylint-land, but the docs appear to disagree.
And I'd say it's clear whose authority is higher.

Thomas

> 
> It just seems wrong, this all or nothing state.
> 
> Steven

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


Re: deprecated string module issue

2010-06-24 Thread Ixokai
On Thu, Jun 24, 2010 at 10:39 AM, GrayShark  wrote:

> Sorry, I meant "from string import lowercase, uppercase"
>
> As I was joining these two, I just changed the import to 'letters'
>
> So if the constants are not deprecated, why is the module? Is some
> other
> state then deprecated needed to describe functions and/or methods
> deprecated,
> but not whole modules?
>
> It just seems wrong, this all or nothing state.
>

Huh?

Again: the string module is not deprecated. It simply is not.

Certain functions in it were deprecated.  Those were clearly marked in the
documentation as deprecated -- since 2.0-ish if memory serves, if not that
early, then not too long into 2.x.

Those deprecated functions have been removed as of 3.x.

If pylint is reporting the entire module is deprecated, pylint is wrong. Its
not deprecated.

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


Re: modifying standard library functionality (difflib)

2010-06-24 Thread Vlastimil Brom
2010/6/24 Bruno Desthuilliers :
> Vlastimil Brom a écrit :

>
> "patching source code" canonically means "physically" modifying the original
> source file. Monkeypatching - which can only be done in some dynamic
> languages - is what you're doing above, ie dynamically replacing a given
> feature at runtime.
>
>>
Thank you very much for the clarification (I indeed didn't consider
this "ultima ratio" approach :-)
Thanks for  the positive suggestion as well.

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


Re: deprecated string module issue

2010-06-24 Thread GrayShark
On Jun 24, 10:06 am, GrayShark  wrote:
> In my code I have:
> from string import lower, upper
>
> When I use pylint on the program I get just one warning:
>
> Uses of a deprecated module 'string'.
>
> Iv'e noted that many if not all string functions are now in _builtin_.
> Where are the constants?
>
> Thanks
> Steven

Sorry, I meant "from string import lowercase, uppercase"

As I was joining these two, I just changed the import to 'letters'

So if the constants are not deprecated, why is the module? Is some
other
state then deprecated needed to describe functions and/or methods
deprecated,
but not whole modules?

It just seems wrong, this all or nothing state.

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


Re: deprecated string module issue

2010-06-24 Thread Stephen Hansen
On Thu, Jun 24, 2010 at 10:06 AM, GrayShark  wrote:

> In my code I have:
> from string import lower, upper
>
> When I use pylint on the program I get just one warning:
>
> Uses of a deprecated module 'string'.
>
> Iv'e noted that many if not all string functions are now in _builtin_.
> Where are the constants?
>

The pylint error is misleading. The string module is not deprecated; all of
the string methods that used to be in it, but are now methods of strings
directly, are.

So instead of lower(your_string), you do your_string.lower().

The constants aren't deprecated (and a couple things have been added to the
module recently, such as the Formatter class).

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


Re: use python as a calculator

2010-06-24 Thread Ian Kelly
On Thu, Jun 24, 2010 at 10:33 AM, ilovesss2004  wrote:
> I use python 2.5
> Must change version? Is there any other solution?

It works for me:

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.0/10**10
1e-010

Perhaps you're doing integer division by mistake?

>>> 1/10**10
0L

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


deprecated string module issue

2010-06-24 Thread GrayShark
In my code I have:
from string import lower, upper

When I use pylint on the program I get just one warning:

Uses of a deprecated module 'string'.

Iv'e noted that many if not all string functions are now in _builtin_.
Where are the constants?

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


Re: use python as a calculator

2010-06-24 Thread Peter Otten
ilovesss2004 wrote:

> If I run
> 1.0/10**10
> python will return 0
> 
> How can I make python return 1e-10?

If you meant 1/10**10, by default this returns an integer in Python 2.x.
With "from __future__ import division" you can opt for the division of 
integers to return a float:

>>> 1/10**10
0
>>> from __future__ import division
>>> 1/10**10
1e-10

Python 3.x will return a float by default.

Peter

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


Re: use python as a calculator

2010-06-24 Thread Etienne Rousee
Le 24/06/2010 18:33, ilovesss2004 a écrit :

> I use python 2.5
> Must change version? Is there any other solution?

With python 2.6.5, I obtain 1e-10.

-- 

Etienne

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


Re: use python as a calculator

2010-06-24 Thread Tim Wintle
On Thu, 2010-06-24 at 09:33 -0700, ilovesss2004 wrote:
> On Jun 24, 5:50 pm, Tim Harig  wrote:
> > On 2010-06-24, ilovesss2004  wrote:
> >
> > > If I run
> > > 1.0/10**10
> > > python will return 0
> >
> > Python 2.6.4 (r264:75706, Dec 11 2009, 23:02:59)
> > [GCC 3.4.6] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >
> > >>> 1.0/10**10
> > 1e-10
> >
> > What version are you using?
> >
> > > How can I make python return 1e-10?
> >
> > If necessary, cast everything to a float:
> >
> >
> >
> >
> >
> > >>> 1.0/float(10**10)
> > 1e-10
> 
> I use python 2.5
> Must change version? Is there any other solution?

Are you sure you're doing the above?

Testing on 2.5:

>>> 1.0/10**10
1e-10

... but if you leave off the ".0" it'll think you want integer
arithmatic, so return 0.

>>> 1/10**10
0

Tim

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


Re: use python as a calculator

2010-06-24 Thread ilovesss2004
On Jun 24, 5:50 pm, Tim Harig  wrote:
> On 2010-06-24, ilovesss2004  wrote:
>
> > If I run
> > 1.0/10**10
> > python will return 0
>
> Python 2.6.4 (r264:75706, Dec 11 2009, 23:02:59)
> [GCC 3.4.6] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> 1.0/10**10
> 1e-10
>
> What version are you using?
>
> > How can I make python return 1e-10?
>
> If necessary, cast everything to a float:
>
>
>
>
>
> >>> 1.0/float(10**10)
> 1e-10

I use python 2.5
Must change version? Is there any other solution?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Should I Learn Python or Ruby next?

2010-06-24 Thread Default User
On Thu, Jun 24, 2010 at 02:53, Gregory Ewing wrote:

> Jean-Michel Pichavant wrote:
>
>  (that makes me think that Perl should be renamed as it outrageously share
>> the same 1st character with Python).
>>
>
> +1. I suggest CalcifiedMolluscSecretion. The very awkwardness
> of that name will doom the language to the obscurity that it
> deserves relative to the One True Language Whose Name Starts
> With P. :-)
>
> --
> Greg
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


May I suggest that you read the Python license.  Really read it.  Then read
the Ruby license.  Really read it.

Ask yourself, if I am just using either internally, and not distributing
anything, does it matter which one I use?

Then, ask yourself, if I am creating code to distribute, especially for
commercial purposes, does it matter which one I use?   Which would you be
more willing to "bet the farm" on (or your house, your career, your food.
etc.)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: modifying standard library functionality (difflib)

2010-06-24 Thread Bruno Desthuilliers

Vlastimil Brom a écrit :


Many thanks for your insights!
Just now, I am the almost the only user of this script, hence the
consequences of version mismatches etc. shouldn't (directly) affect
anyone else, fortunately.


So far so good.


However, I'd like to ask for some clarification about monkeypatching -
With "directly replace" I  meant something like the following scenario:

import difflib

def tweaked__chain_b(self):
# modified code of the function __chain_b copy from Lib\difflib.py
...

difflib.SequenceMatcher._SequenceMatcher__chain_b = tweaked__chain_b

I thought, this would qualify as monkeypatching,


It does, indeed


but I am apparently
missing some distinction between "patching the ... code inplace"  and
"monkeypatching".


"patching source code" canonically means "physically" modifying the 
original source file. Monkeypatching - which can only be done in some 
dynamic languages - is what you're doing above, ie dynamically replacing 
a given feature at runtime.



By subclassing (which I am using just now in the code)


If it already works and you don't have to care too much about possible 
compat issues with different difflib versions, then look no further.


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


Re: use python as a calculator

2010-06-24 Thread D'Arcy J.M. Cain
On Thu, 24 Jun 2010 08:42:32 -0700 (PDT)
ilovesss2004  wrote:
> If I run
> 1.0/10**10
> python will return 0
> 
> How can I make python return 1e-10?

What version of Python are you using?

Python 2.6.4 (r264:75706, Jan 28 2010, 11:26:00) 
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd7
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.0/10**10
1e-10

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use python as a calculator

2010-06-24 Thread Tim Harig
On 2010-06-24, ilovesss2004  wrote:
> If I run
> 1.0/10**10
> python will return 0

Python 2.6.4 (r264:75706, Dec 11 2009, 23:02:59) 
[GCC 3.4.6] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.0/10**10
1e-10
>>> 

What version are you using?

> How can I make python return 1e-10?

If necessary, cast everything to a float:

>>> 1.0/float(10**10)
1e-10
>>> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use python as a calculator

2010-06-24 Thread Thomas Jollans
On 06/24/2010 05:42 PM, ilovesss2004 wrote:
> If I run
> 1.0/10**10
> python will return 0
> 
> How can I make python return 1e-10?

It returns 1e-10.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use python as a calculator

2010-06-24 Thread David Houston
I believe this is fixed in python3

On Thu, Jun 24, 2010 at 4:42 PM, ilovesss2004  wrote:

> If I run
> 1.0/10**10
> python will return 0
>
> How can I make python return 1e-10?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Dave
-
Email: r...@crankyadmin.net
Web: www.crankyadmin.net
-- 
http://mail.python.org/mailman/listinfo/python-list


use python as a calculator

2010-06-24 Thread ilovesss2004
If I run
1.0/10**10
python will return 0

How can I make python return 1e-10?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Book review / advise

2010-06-24 Thread Stephen Hansen
On Wed, Jun 23, 2010 at 10:22 AM, John Bokma  wrote:

> Stephen Hansen  writes:
>
> > On 6/22/10 9:48 PM, John Bokma wrote:
> > Its when you package it up in such a way that the buyer doesn't realize
> > what they're buying, that's where the problem comes-- and that's what is
> > happening quite a lot these days.
>
> Which is not the case here.
>

Says you? Oh, well, that settles it. Good then, we're all clear now.

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


Re: print line number and source filename

2010-06-24 Thread Stephen Hansen
On Wed, Jun 23, 2010 at 8:07 PM, Peng Yu  wrote:

> I tried to put the above code in a module. Say in a.b.__init__.py
>
> %(module)s only print to "__init__". However, I need the fullname
> a.b.__init__. I looked at the manual, but I don't see what format
> string I should supply. Would you please let me know?


If you look at the manual you'll find:

http://docs.python.org/library/logging.html#formatter

To which %(pathname)s appears to be what you're looking for.

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


Re: Types missing from "types"module

2010-06-24 Thread Thomas Jollans
On 06/24/2010 08:29 AM, John Nagle wrote:
> On 6/23/2010 10:08 PM, Stephen Hansen wrote:
>> On Jun 23, 2010, at 9:24 PM, John Nagle  wrote:
>>
>>>   Here's dir(types), in Python 2.6.5:
>>>
>>> ['BooleanType', 'BufferType', 'BuiltinFunctionType',
>>> 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType',
>>> 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType',
>>> 'FileType', 'FloatType', 'FrameType', 'FunctionType',
>>> 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType',
>>> 'LambdaType', 'ListType', 'LongType', 'MemberDescriptorType',
>>> 'MethodType', 'ModuleType', 'NoneType','NotImplementedType',
>>> 'ObjectType', 'SliceType', 'StringType', 'StringTypes',
>>> 'TracebackType', 'TupleType', 'TypeType', 'UnboundMethodType',
>>> 'UnicodeType', 'XRangeType', '__builtins__', '__doc__', '__file__',
>>> '__name__', '__package__']
>>>
>>> Seems to be missing SetType, FrozenSetType, BytesType, and
>>> ByteArrayType.  Anything else missing?
>>>
>>> (Arguably, "bytes" isn't really distinguished until 3.x, but
>>> still...)
>>
>> IIUC, since Python 2.2ish you can't treat the types module as
>> comprehensive. It exists as a remnant from the time when there was a
>> difference between types and classes.
>>
>> Sets and the recent additions exist solely in this new world where we
>> just isinstance(blah, set) or issubclass or whatnot.
> 
>Ah.  That makes sense.
> 
>Does the "types" module go away in 3.x, then?

No, because while types like dict, int, and tuple are part of the core
language, other types aren't directly accessible.

>>> import types
>>> dir(types)
['BuiltinFunctionType', 'BuiltinMethodType', 'CodeType', 'FrameType',
'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'LambdaType',
'MemberDescriptorType', 'MethodType', 'ModuleType', 'TracebackType',
'__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>>


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


Re: modifying standard library functionality (difflib)

2010-06-24 Thread Vlastimil Brom
2010/6/24 Bruno Desthuilliers :
> Vlastimil Brom a écrit :
>>
>> Hi all,
>> I'd like to ask about the most reasonable/recommended/... way to
>> modify the functionality of the standard library module (if it is
>> recommended at all).
>
> ...
>> - I guess, it wouldn't be recommended to directly replace
>> difflib.SequenceMatcher._SequenceMatcher__chain_b ...
>
> For which definition of "directly replace" ? If you mean patching the
> standardlib's source code inplace, then it's definitly not something i'd do.
>  Monkeypatching OTHO is sometimes the simplest solution, specially for
> temporary fixes or evolutions.
>
> Anyway - which solution (forking, subclassing or monkeypatching) is the most
> appropriate really depends on the context so only you can decide. If it's
> for personal use only and not mission-critical, go for the simplest working
> solution. If it's going to be publicly released, you may want to consider
> contacting the difflib maintainer and submit a patch, and rely on a
> monkeypatch in the meantime. If you think you'll have a need for more
> modifications / specialisations / evolution to difflib, then just fork.
>
> My 2 cents.
> --
>

Many thanks for your insights!
Just now, I am the almost the only user of this script, hence the
consequences of version mismatches etc. shouldn't (directly) affect
anyone else, fortunately.
However, I'd like to ask for some clarification about monkeypatching -
With "directly replace" I  meant something like the following scenario:

import difflib

def tweaked__chain_b(self):
# modified code of the function __chain_b copy from Lib\difflib.py
...

difflib.SequenceMatcher._SequenceMatcher__chain_b = tweaked__chain_b

this way I can only unconditionally change the functionality, as the
signature of SequenceMatcher (which is then used in my script) remains
unchanged.

I thought, this would qualify as monkeypatching, but I am apparently
missing some distinction between "patching the ... code inplace"  and
"monkeypatching".
Is it maybe a difference, if one makes "backups" of the original
objects and reactivates them after the usage of the patched code?

By subclassing (which I am using just now in the code) the behaviour
can be parametrised:

class my_difflib_SequenceMatcher(difflib.SequenceMatcher):
def __init__(self, isjunk=None, a='', b='', checkpopular=True):
# checkpopular added parameter to the signature
self.checkpopular = checkpopular
 ...

def __chain_b(self):
# modified copy from Lib\difflib.py - reacting to the value of
self.checkpopular

An "official" update of the source in the standard library is probably
not viable (at least not in a way that would currently help me, as my
code only supports python 2.x due to the relevant dependencies
(wxpython )
Otherwise, it would depend on other users' needs (e.g. finer diff at
the cost of the much slower code in some cases )

Thanks again for your thoughts.
   vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another MySQL Problem

2010-06-24 Thread Victor Subervi
On Thu, Jun 24, 2010 at 9:34 AM, Stephen Hansen wrote:

> On Thu, Jun 24, 2010 at 4:47 AM, Victor Subervi 
> wrote:
>
>> On Thu, Jun 24, 2010 at 1:56 AM, John Nagle  wrote:
>>
> Yes.  Please post your CREATE statements, so we can see your
>>> database schema.  If you really have one table per client, you're
>>> doing it wrong.
>>>
>>
>> cursor.execute('create table if not exists clients ('client varchar(100),
>> clientAppelation varchar(10), clientFirst varchar(20), clientLast
>> varchar(40), clientEmail varchar(60), pw varchar(10)')
>>
>
> That's only a partial picture: your code later indicated that you have
> tables which are named for client. Like, "Lincoln_Properties", or some such.
> That's the problem: you have data which belongs in a field (Lincoln) in the
> table name.
>
> Basically, instead of:
>
> Lincoln_Properties
> Memphis_Properties
> Angel_Properties
>
> And such.
>
> You should have a single "Properties" table, which is identical to the
> schema of the above: but with an additional field, "client", which is either
> "Lincoln" or "Memphis" or whatnot. You may then need to tweak your primary
> keys a bit.
>
> Then instead of "cur.execute('select * from %s', client)" which errored out
> -- because you can't use parameterized queries for schema objects (tables,
> fields, etc) -- you would simply change it to "cur.execute('select * from
> properties where client = %s', (client,))" -- and it'd work just the same.
>

Obvious in retrospect. Thanks for cluing me in :))
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Classes

2010-06-24 Thread Nathan Rice
As others have mentioned when you would like to encapsulate data and
functions together, they're useful.

Also, if you find yourself passing lists/tuples/dictionaries around
frequently, you might benefit from converting them into classes.  This tends
to make the code clearer and more readable, you can refer to things by
attribute name instead of index without unpacking, and you can create custom
processing logic around attributes if need be.

I also have an esoteric use case for classes, when I have a process that is
implemented very slightly differently in several situations.  I break the
elements of a function up into atomic components as methods on a class, then
override the variable parts in subclasses.  I find that this lets me re-use
more code than writing multiple different top-level functions.

On Thu, Jun 24, 2010 at 7:58 AM, Mag Gam  wrote:

> I have been using python for about 1 year now and I really like the
> language. Obviously there was a learning curve but I have a programing
> background which made it an easy transition. I picked up some good
> habits such as automatic code indenting :-), and making my programs
> more modular by having functions.
>
> I know that Python is very OOP friendly, but I could not figure out
> why and when to use Classes in it. I mostly use it for simple text
> parsing  I suppose when a program gets "complicated" I should start
> using Classes. Are there any tips or tricks people use to "force" them
> into the OOP mentality? I would like to force myself to learn the
> Python way but so far I could not figure out WHY I would need a class
> for this...
>
>
> TIA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to send a non-standard IMAP command?

2010-06-24 Thread Tim Chase

On 06/24/2010 04:47 AM, Xianwen Chen wrote:

Thanks a lot for your reply! I thought it would be simpler if the
problem was presented in a brief way. Unfortunately, not for this
case.

Here is the detail. Free Yahoo! mail accounts can be accsessed via
IMAP protocal, however, a non-standard shake hand code is needed
before log in [1]:

ID ("GUID" "1")

. This is what I'm now working for. I tried:

IMAP4.xatom('','ID ("GUID" "1")','',)

and

dest_srv.xatom('ID ("GUID" "1")')

, but I got error messages. Any hint please?


In general, it would be helpful to include the error-message(s) 
you get.  However, I tried it with a junk Yahoo account I set up:


  from imaplib import IMAP4
  i = IMAP4("imap.mail.yahoo.com")
  USER = 'yourusern...@yahoo.com'
  PASS = 'your secret goes here'
  # per the Wikipedia page you gave
  # the ID has to happen before login
  i.xatom('ID ("GUID" "1")')

  i.login(USER, PASS)
  i.select()
  typ, data = i.search(None, 'ALL')
  for num in data[0].split():
typ, data = i.fetch(num, '(RFC822)')
message = data[0][1].splitlines()
subject = [line
  for line in message
  if line.lower().startswith('subject: ')
  ][0]
print num, subject
  i.close()
  i.logout()

and it worked.

-tkc



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


Re: How to send a non-standard IMAP command?

2010-06-24 Thread Michael Torrie
On 06/24/2010 03:47 AM, Xianwen Chen wrote:
> , but I got error messages. Any hint please?

Why not just use a proxy server:

http://sourceforge.net/projects/imapidproxy/


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


Re: How to send a non-standard IMAP command?

2010-06-24 Thread Michael Torrie
On 06/24/2010 03:47 AM, Xianwen Chen wrote:
> . This is what I'm now working for. I tried:
> 
> IMAP4.xatom('','ID ("GUID" "1")','',)
> 
> and
> 
> dest_srv.xatom('ID ("GUID" "1")')
> 
> , but I got error messages. Any hint please?

What error messages?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another MySQL Problem

2010-06-24 Thread Stephen Hansen
On Thu, Jun 24, 2010 at 4:47 AM, Victor Subervi wrote:

> On Thu, Jun 24, 2010 at 1:56 AM, John Nagle  wrote:
>
Yes.  Please post your CREATE statements, so we can see your
>> database schema.  If you really have one table per client, you're
>> doing it wrong.
>>
>
> cursor.execute('create table if not exists clients ('client varchar(100),
> clientAppelation varchar(10), clientFirst varchar(20), clientLast
> varchar(40), clientEmail varchar(60), pw varchar(10)')
>

That's only a partial picture: your code later indicated that you have
tables which are named for client. Like, "Lincoln_Properties", or some such.
That's the problem: you have data which belongs in a field (Lincoln) in the
table name.

Basically, instead of:

Lincoln_Properties
Memphis_Properties
Angel_Properties

And such.

You should have a single "Properties" table, which is identical to the
schema of the above: but with an additional field, "client", which is either
"Lincoln" or "Memphis" or whatnot. You may then need to tweak your primary
keys a bit.

Then instead of "cur.execute('select * from %s', client)" which errored out
-- because you can't use parameterized queries for schema objects (tables,
fields, etc) -- you would simply change it to "cur.execute('select * from
properties where client = %s', (client,))" -- and it'd work just the same.

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


Re: question about multiprocessing

2010-06-24 Thread hywhy

thanks,friend!
I wanna use Queue to share objects,but on windows, the multiprocessing
module can‘t do this。 Is there any way to solve this problem!
thanks
-- 
View this message in context: 
http://old.nabble.com/question-about-multiprocessing-tp28940614p28982744.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: Problem PIL-1.1.7 & _imagingft

2010-06-24 Thread Christian Heimes
Am 24.06.2010 14:39, schrieb Michel Claveau - MVP:
> Hi!
> 
> With PIL-1.1.7.win32-py2.6 and Windows 7, I have this traceback:
>   raise ImportError("The _imagingft C module is not installed")
>   ImportError: The _imagingft C module is not installed
> 
> I tested "import _imaging" and several other things, without success.
> If I re-install PIL-1.1.6  all run OK.
> 
> I found a solution:
>  - install PIL-1.1.7,
>  - then, copy these two files from PIL 1.1.6: _imaging.pyd & _imagingft.pyd

The _imageft.pyd DLL of PIL 1.1.7 has a erroneous dependency on the
Debug CRT. You can safely remove the dependency by editing the file with
a hex editor. Open the file on your hex editor, search for the string
"assembly" and comment out the Debug CRT. The block should look like:

  

Christian

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


Re: Emacs Form Feed (^L) Display Suggestion and Tips

2010-06-24 Thread Xah Lee
On Jun 24, 5:41 am, Xah Lee  wrote:
> • Emacs Form Feed (^L) Display Suggestion and Tips
> http://xahlee.org/emacs/modernization_formfeed.html

a follow up question.

when i was learning python in ~2005, i remember seeing it in python
code, but i haven't done much python since. Does python source code
still have that now and then? Is there some official guide on marking
sections?

also, besides emacs elisp, does anyone see the form feed char in other
lang source code?

thanks.

  Xah
∑ http://xahlee.org/

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


Re: Using Classes

2010-06-24 Thread Alf P. Steinbach /Usenet

* Mag Gam, on 24.06.2010 13:58:

I have been using python for about 1 year now and I really like the
language. Obviously there was a learning curve but I have a programing
background which made it an easy transition. I picked up some good
habits such as automatic code indenting :-), and making my programs
more modular by having functions.

I know that Python is very OOP friendly, but I could not figure out
why and when to use Classes in it. I mostly use it for simple text
parsing  I suppose when a program gets "complicated" I should start
using Classes. Are there any tips or tricks people use to "force" them
into the OOP mentality? I would like to force myself to learn the
Python way but so far I could not figure out WHY I would need a class
for this...


Use whatever paradigm that yields code that you easily understand. 

Having said that, the main use of a class is to model some data with an 
associated set of operations.


For this a class is merely a convenience, but sometimes the convenience can be 
so great that any other approach would be impractical in comparision.


In some cases a non-class approach ends up with a number of routines foo, bar, 
zlopf etc. that all take some argument of a "general" type and all internally 
has some "if it's really a BRACHYKLURB, do A, assuming that it has 
BRACHYKLURB-specific data, but if it's really a KNETCHOFICHUS, then do B, 
assuming that it has KNETCHOFICHUS-specific data, and as default, if it's none 
of those, do C".


This is a maintainance nightmare. You have to be sure that every such routine 
discriminates correctly on type, checking all possibilities and doing the right 
thing for each possibility. And you have to be sure of that in the face of 
possible additions of new subtypes, or removal of a subtype (maintainance).


Then the answer is to introduce some classes and OOP stuff.

In essence, instead of letting general routines awkwardly and error-prone choose 
the right type-specific routines depending on the object, let the object specify 
the right type-specific routines directly. :-)



Cheers & hth.,

- Alf

--
blog at http://alfps.wordpress.com>
--
http://mail.python.org/mailman/listinfo/python-list


Emacs Form Feed (^L) Display Suggestion and Tips

2010-06-24 Thread Xah Lee
• Emacs Form Feed (^L) Display Suggestion and Tips
  http://xahlee.org/emacs/modernization_formfeed.html

plain text version follows
--

Emacs Form Feed (^L) Display Suggestion and Tips

Xah Lee, 2010-06-24

This page discusses some problems involving the Form feed character
(^L) in emacs, and gives you some tips for better solution.

In emacs lisp source code, sometimes you'll see “^L”. That's the “form
feed” character, ascii 12. For example, type “Alt+x describe-
function”, then dired, then click on the “dired.el” link to view the
source code. Scroll around and you'll see it.

--
Problem

The form feed char is used in 1990s or earlier to cause printer to
start with a new page. Printers today no longer use that in their
protocol. However, sometimes it is also used by programer as a section
marker. Many emacs lisp source code still have it. It is also
sometimes seen in Python source code.

The displaying of ^L is hard to read, and is mysterious to modern
programers.

--
Solution

You can make emacs display a horizontal line instead. You need to
install Pretty Control-L minor mode, by Drew Adams. Download at:
emacswiki.org PrettyControlL.

To install, place the file in your “~/.emacs.d/” dir, then put the
following in your emacs init file (“~/.emacs”):

;; add the dir to load path
(add-to-list 'load-path "~/.emacs.d/")

;; display horizontal line for the Form Feed char (ASCII 12, ^L) The
;; Form Feed char is often used in elisp source code for marking
;; sections. The command forward-page (and backward-page) moves to the
;; next form feed char.
(require 'pp-c-l)
(setq pp^L-^L-string
"   ")
(pretty-control-l-mode 1)

By default, it'll display a line, but also with the annoying text
“Section (Printable Page)”.

The line “(setq pp^L-^L-string ...)” above solves that problem.

--
Hotkey to Jump to Form Feed

Also, emacs support a hotkey to jump to the form feed char. By
default, the key is “C-x [” and “C-x ]”. Very hard to use and hard to
remember. You can set a more convenient key. For example:

;; shortcut for moving to prev/next form feed
(global-set-key (kbd "") 'forward-page) ; Alt+Shift
+PageDown
(global-set-key (kbd "") 'backward-page) ; Alt+Shift+PageUp

Note that emacs already uses Ctrl+Page up/down for scrolling left/
right, and Meta+page up/down for paging up/down the next pane, and
Shift+Page up/down selects text. You should not use Ctrl+Shift combo
because Ctrl+Shift+‹letter› is avoided because text based terminals
have problems distinguishing the key from non-shifted letters.

If you have Hyper key set, you can use the following more easier keys:

(global-set-key (kbd "") 'forward-page)
(global-set-key (kbd "") 'backward-page)

If you don't use the number pad keys, you can set it to those keys,
like this:

(global-set-key (kbd "") 'backward-page) ; keypad 9
(global-set-key (kbd "") 'forward-page) ; keypad 6

For how to set up hyper key and others, see: How to Define Keyboard
Shortcuts in Emacs.

--
Typing the Form Feed Char

In emacs, you can type the char by pressing “Ctrl+q” then “Ctrl+l”.

(For explanation of this, see: The Confusion of Emacs's Keystroke
Representation.)

--
Advantages of Using Form Feed As Section Marker in Source Code

Normally, programer mark section in source code with lots of comment
chars. For example:

;--

#__

###

///

These are not as elegant or convenient as the form feed char. First
problem is typing them. Even if your editor provide a easy way to type
the char repeatedly, such as emacs's “C-u 50” prefix, that's still 4
or 5 extra keys to press.

It is also hard to search. Because the style varies, some source code
repeat the comment chars such as “##”, some start with a
comment char then followed by dashes “#--”, some uses
underline, some draws a ascii art box, like this:

##
#  functions #
##

All these variations makes it hard to jump to the section. Typically,
you search the string, for example, search a sequence of 5 #, but
because the line has more than 5, your search will linger on the same
line. Instead, you have to search almost the exact the number of chars
used for this, and it is common that the number of chars for such line
is not consistent. Or, you may use regex, but again, much more typing
and the result is not precise.

The form feed char has several advantages over this. If you use it for
section mark, it makes it precise, easy to locate, and easy to

Re: Pythonic Idiom For Searching An Include Path

2010-06-24 Thread Tim Daneliuk
On 6/24/2010 2:57 AM, Gregory Ewing wrote:
> Tim Daneliuk wrote:
>>   "If 'includefile' contains one or more path separator characters,
>>ignore 'FOOPATH'.
> 
> Are you sure that's exactly what you want? Usually with
> such things the distinction is absolute vs. relative,
> not whether there is more than one pathname component.
> E.g. in a C file,
> 
> #include "GL/gl.h"
> 
> will search the include path for a directory called
> "GL" containing a file called "gl.h".
> 

Point well taken, thanks.

-- 

Tim Daneliuk tun...@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

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


Problem PIL-1.1.7 & _imagingft

2010-06-24 Thread Michel Claveau - MVP
Hi!

With PIL-1.1.7.win32-py2.6 and Windows 7, I have this traceback:
  raise ImportError("The _imagingft C module is not installed")
  ImportError: The _imagingft C module is not installed

I tested "import _imaging" and several other things, without success.
If I re-install PIL-1.1.6  all run OK.

I found a solution:
 - install PIL-1.1.7,
 - then, copy these two files from PIL 1.1.6: _imaging.pyd & _imagingft.pyd

But, is it a good way?

Thanks
-- 
Michel Claveau

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


Re: Using Classes

2010-06-24 Thread Neil Cerutti
On 2010-06-24, Bruno Desthuilliers
 wrote:
> Not going into OO when you don't need it IS actually the
> "Python way" !-)

My most satisfying classes are the ones that gradually coalesce
from a functional program. They take actual shape during
refactoring.

I find classes to be the most successful when they improve my
conception of a program by gathering operations and data together
in a way that simplifies my program.

They have the least positive impact when I slap them together
early on, just to group up some data that appears to need
grouping.

It's pretty rare that I know the best operations for each bit of
data and how best to bundle that data until after a program is
functional. It is lucky my programs are relatively small. ;)

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


Re: best way to increment an IntVar?

2010-06-24 Thread Bruno Desthuilliers

Dennis Lee Bieber a écrit :
(snip - about Tkinter IntVar type)


It is NOT a numeric "variable" in Python realms.

So var+=increment can't be used because Python would rebind the name
var to a new object -- but Tkinter would still be hooked to the original
object and never see the change.


>>> foo = []
>>> bar = foo
>>> foo += [1]
>>> foo
[1]
>>> bar
[1]
>>> bar is foo
True
>>> foo.__iadd__([2])
[1, 2]
>>> _ is foo
True
>>>

IOW : __iadd__ doesn't necessarily returns a new object !-)



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


Re: Using Classes

2010-06-24 Thread Bruno Desthuilliers

Mag Gam a écrit :

I have been using python for about 1 year now and I really like the
language. Obviously there was a learning curve but I have a programing
background which made it an easy transition. I picked up some good
habits such as automatic code indenting :-), and making my programs
more modular by having functions.

I know that Python is very OOP friendly, but I could not figure out
why and when to use Classes in it.


If you have some more or less formal "data type" defined by ie dicts 
with some particular keys in them or tuples with a known structure, and 
a few fonctions working on these dicts or tuples, then you have a 
perfect use case for OO.


For other use case, have a look at the csv module or the various XML / 
SGML / HTML parsers in the stdlib. There's a pretty interesting paper 
from Alex Martelli here:


http://www.aleax.it/Python/os03_template_dp.pdf



I mostly use it for simple text
parsing  I suppose when a program gets "complicated" I should start
using Classes.


Not necessarily. OO is one way to organize code and data, but there are 
other ways that work as well, depending on the problem at hand and how 
your brain is connected.



Are there any tips or tricks people use to "force" them
into the OOP mentality? I would like to force myself to learn the
Python way but so far I could not figure out WHY I would need a class
for this...


Not going into OO when you don't need it IS actually the "Python way" !-)
--
http://mail.python.org/mailman/listinfo/python-list


dbus and properties multivalue

2010-06-24 Thread ste
hello,
 i want to build a simple script to resize a window of konqueror using
the
dbus protocol. Normally use the python interface, but the problem is
most
general because this operation it is impossible for me also if use
qdbusviewer
and is to complex for me if use dbus-send.

If i use the python interface to set a property with only a value
(example:
'minimumWidth') all is ok with this code

theObject = dbus.SessionBus().getObject('org.kde.konqueror-4561',
'/konqueror/MainWindow_1')
theInterface = (theObject,
dbus_interface='org.freedesktop.DBUS.Properties')
theInterface.Set('com.trolltech.Qt.QWidget', 'minimumWidth', 298)

If i check with qdbusviewer, the value of property 'minimumWidth'
change
correctly.

Try the same operation with 'geometry' property that accept a
parameter
'()' and receive the exception
org.freedesktop.DBus.Error.InvalidArgs
maybe because i don't know how format the correct parameter.
The same response i have if use as parameter the answer of the same
Get
operation:
value = theInterface.Get('com.trolltech.Qt.QWidget', 'geometry')
theInterface.Set('com.trolltech.Qt.QWidget', 'geometry', value)

thanks for the attention
-- 
http://mail.python.org/mailman/listinfo/python-list


Using Classes

2010-06-24 Thread Mag Gam
I have been using python for about 1 year now and I really like the
language. Obviously there was a learning curve but I have a programing
background which made it an easy transition. I picked up some good
habits such as automatic code indenting :-), and making my programs
more modular by having functions.

I know that Python is very OOP friendly, but I could not figure out
why and when to use Classes in it. I mostly use it for simple text
parsing  I suppose when a program gets "complicated" I should start
using Classes. Are there any tips or tricks people use to "force" them
into the OOP mentality? I would like to force myself to learn the
Python way but so far I could not figure out WHY I would need a class
for this...


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


Re: Another MySQL Problem

2010-06-24 Thread Victor Subervi
On Thu, Jun 24, 2010 at 1:56 AM, John Nagle  wrote:

> On 6/23/2010 10:59 PM, Dennis Lee Bieber wrote:
>
>> On Wed, 23 Jun 2010 11:58:24 -0430, Victor Subervi
>>   declaimed the following in
>> gmane.comp.python.general:
>>
>>
>>  When I have this code:
>>>
>> 
>
>
>>And yes -- it IS an error... You are AGAIN trying to use parameters
>> for SCHEMA entities, not data.
>>
>
>Yes.  Please post your CREATE statements, so we can see your
> database schema.  If you really have one table per client, you're
> doing it wrong.
>

cursor.execute('create table if not exists clients ('client varchar(100),
clientAppelation varchar(10), clientFirst varchar(20), clientLast
varchar(40), clientEmail varchar(60), pw varchar(10)')

TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple list problem that's defeating me!

2010-06-24 Thread Bruno Desthuilliers

Neil Webster a écrit :

Thanks for the help so far.

The background to the problem is that the lists come from reading a
dbf file.  The code that I am trying to write is to merge lines of the
dbf based on the first column.  So in my example there would be three
lines:
a 2 3 4
b 10 11 12
a 2 3 4

The expected output from the above example lines would be:
a 4 6 8
b 10 11 12

 ... and the lines are read as: [[a,2,3,4],[b,10,11,12], [a,2,3,4]]



If you don't care about the original ordering, the following code should 
do.


# 8<

def merge_rows(rows):
merged = dict()
for row in rows:
key, values = row[0], row[1:]
sums = merged.setdefault(key, [0, 0, 0])
for i, v in enumerate(values):
sums[i] += v

return [key] + value for key, value in merged.iteritems()]

import sys
def print_rows(rows, out=sys.stdout):
for row in rows:
print " ".join(map(str, row))

if __name__ == '__main__':
inputs = [['a',2,3,4],['b',10,11,12], ['a',2,3,4]]
expected = [['a',4,6,8],['b',10,11,12]]
print "inputs : "
print_rows(inputs)

outputs = merge_rows(inputs)
outputs.sort() # so we can do a simple equality test
assert outputs == expected, "Expected %s, got %s" % (
   expected, outputs
   )

print "outputs :"
print_rows(outputs)

# 8<



In response to not posting working code or actual inputs, ummm, that's
why I am asking the question here.


In this context, "working code" means "minimal code that a) can be 
executed and b) exhibits the problem you have, whatever the problem is". 
This code should include or be shipped with example input data and 
corresponding expected output data - as I did in the above code.



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


Re: How to send a non-standard IMAP command?

2010-06-24 Thread Xianwen Chen
On 6/24/10, Tim Chase  wrote:
> On 06/23/2010 05:22 PM, Xianwen Chen wrote:
>> I need to send one line of commands to an IMAP server. The commands
>> are not standard IMAP protocols, hence it's not specified in
>> http://docs.python.org/library/imaplib.html.
>
> Sounds like you want to use the imaplib's IMAP4.xatom() to send a
> custom command to your IMAP server:
>
> http://docs.python.org/library/imaplib.html#imaplib.IMAP4.xatom
>
> Given that you don't detail what you want to send or expect back,
> it's a little hard to give you more than that, but at least that
> should get you started.  However, the full source to imaplib is
> in your $PYTHONLIB directory so you can see how other commands
> are implemented and responses are interpreted.
>
> -tkc
>
>
>
>
>
>
>
>

Hi Tim,

Thanks a lot for your reply! I thought it would be simpler if the
problem was presented in a brief way. Unfortunately, not for this
case.

Here is the detail. Free Yahoo! mail accounts can be accsessed via
IMAP protocal, however, a non-standard shake hand code is needed
before log in [1]:

ID ("GUID" "1")

. This is what I'm now working for. I tried:

IMAP4.xatom('','ID ("GUID" "1")','',)

and

dest_srv.xatom('ID ("GUID" "1")')

, but I got error messages. Any hint please?

Best regards,

Xianwen

[1] http://en.wikipedia.org/wiki/Yahoo!_Mail#Free_IMAP_and_SMTPs_access

-- 
Xianwen Chen
University of Tromsø
http://u.nu/9uubc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple list problem that's defeating me!

2010-06-24 Thread Sion Arrowsmith
Mark Lawrence   wrote:
>On 22/06/2010 15:06, Neil Webster wrote:
>> I have a list of lists such as [[a,2,3,4],[b,10,11,12], [a,2,3,4]].  I
>> need to combine the two lists that have the same first character in
>> this example 'a'.  In reality there are 656 lists within the list.
>> [ ... ]
>My simplistic approach.
>
>Sort the list (this happens in place).
>Use the itertools groupby function to place everything together, see
>http://docs.python.org/library/itertools.html?highlight=groupby#itertools.groupby
>Combine the lists in the groups.

I suspect the following is a more efficient way of acheiving the grouping:

d = collections.defaultdict(list)
for a in L:
d[a[0]].append(a[1:])

-- 
\S

   under construction

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


Re: Simple list problem that's defeating me!

2010-06-24 Thread Neil Webster
Thanks for the help so far.

The background to the problem is that the lists come from reading a
dbf file.  The code that I am trying to write is to merge lines of the
dbf based on the first column.  So in my example there would be three
lines:
a 2 3 4
b 10 11 12
a 2 3 4

The expected output from the above example lines would be:
a 4 6 8
b 10 11 12

 ... and the lines are read as: [[a,2,3,4],[b,10,11,12], [a,2,3,4]]

In response to not posting working code or actual inputs, ummm, that's
why I am asking the question here.








On Jun 22, 4:38 pm, Bruno Desthuilliers  wrote:
> Neil Webster a crit :
>
> > Hi all,
>
> > I've got a simple problem but it's defeated me and I was wondering if
> > somebody could point out where I'm going wrong
>
> 1/ not posting working code (got a NameError)
> 2/ not posting the expected output
> 3/ not posting the actual output
>
> > or offer an alternative
> > solution to the problem?
>
> When you'll have fixed the 3 problems listed above !-)
>
> (snip broken code)

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


Re: ttk Scale: missing attributes

2010-06-24 Thread eb303
On Jun 23, 7:26 pm, Alan G Isaac  wrote:
> Tkinter's Scale widget had a `label` and a `resolution` attribute.
> These appear to be missing from the Ttk Scale widget.
> Is there a reason?  These were important attributes.
>
> Thanks,
> Alan Isaac

As you might know, the new ttk widgets are not intenteded to be drop-
in replacements for older tk widgets, so some of them do not have the
same options indeed. For your particular problem, the options were
probably dropped because they are not absolutely needed: you can do by
yourself what the 'resolution' option did automatically, and you can
just add a label widget near the scale if you want to. Just a guess…
 - Eric -
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: example of multi threads

2010-06-24 Thread Sandy
On Jun 23, 11:58 am, Stefan Behnel  wrote:
> Mag Gam, 23.06.2010 12:24:
>
> > I am looking for a simple multi threaded  example.
>
> > Lets say I have to ssh to 20 servers and I would like to that in
> > parallel. Can someone please provide a an example for that?
>
> Sounds like you want to run background processes, not threads. Take a look
> at the subprocess module.
>
> Stefan

I use ssh which in turn uses paramiko

  import os
  import ssh
  for i in xrange(len(tasks)):
pid = os.fork()
if pid == 0:
# if child process
s = ssh.Connection(host = server, log =
False)
s.execute(cmd)
s.close()
os._exit(0)
time.sleep(1)

  os.waitpid(pid, 0)

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


Re: the bugerrd code

2010-06-24 Thread Bruno Desthuilliers

Victoria Hernandez a écrit :

The new mision I herits the buggered code (i do not the bugger). How
do debugger him? Tahnk you very much. Vikhy :)


http://docs.python.org/library/pdb.html#module-pdb
http://docs.python.org/library/unittest.html#module-unittest
--
http://mail.python.org/mailman/listinfo/python-list


Re: modifying standard library functionality (difflib)

2010-06-24 Thread Bruno Desthuilliers

Vlastimil Brom a écrit :

Hi all,
I'd like to ask about the most reasonable/recommended/... way to
modify the functionality of the standard library module (if it is
recommended at all).


(snip)

However, I'd like to ask, how to best maintain this modified
functionality in the sourcecode.
I tried some possibilities, which seem to work, but I'd appreciate
suggestions on the preferred way in such cases.
- It is simply possibly to have a modified sourcefile difflib.py in
the script directory.


You'd better do real fork then and rename the damn thing to avoid 
confusions and name shadowing.




- Furthermore one can subclass difflib.SequenceMatcher an overide its
__chain_b function (however the name doesn't look like a "public"
function ...


It's indeed a very "private" one. Beware of name mangling here, can lead 
to surprising results !-)


Also, overriding an implementation method, your code might break with 
each new release, so it kind of tie you to a specific version (or set 
of...). The odds depend on difflib's source code stability.



- I guess, it wouldn't be recommended to directly replace
difflib.SequenceMatcher._SequenceMatcher__chain_b ...


For which definition of "directly replace" ? If you mean patching the 
standardlib's source code inplace, then it's definitly not something i'd 
do.  Monkeypatching OTHO is sometimes the simplest solution, specially 
for temporary fixes or evolutions.


Anyway - which solution (forking, subclassing or monkeypatching) is the 
most appropriate really depends on the context so only you can decide. 
If it's for personal use only and not mission-critical, go for the 
simplest working solution. If it's going to be publicly released, you 
may want to consider contacting the difflib maintainer and submit a 
patch, and rely on a monkeypatch in the meantime. If you think you'll 
have a need for more modifications / specialisations / evolution to 
difflib, then just fork.


My 2 cents.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic Idiom For Searching An Include Path

2010-06-24 Thread Gregory Ewing

Tim Daneliuk wrote:

  "If 'includefile' contains one or more path separator characters,
   ignore 'FOOPATH'.


Are you sure that's exactly what you want? Usually with
such things the distinction is absolute vs. relative,
not whether there is more than one pathname component.
E.g. in a C file,

#include "GL/gl.h"

will search the include path for a directory called
"GL" containing a file called "gl.h".

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


Re: Should I Learn Python or Ruby next?

2010-06-24 Thread Gregory Ewing

Jean-Michel Pichavant wrote:

(that makes me think that Perl should be renamed as it 
outrageously share the same 1st character with Python).


+1. I suggest CalcifiedMolluscSecretion. The very awkwardness
of that name will doom the language to the obscurity that it
deserves relative to the One True Language Whose Name Starts
With P. :-)

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


Re: Pythonic Idiom For Searching An Include Path

2010-06-24 Thread Nobody
On Wed, 23 Jun 2010 17:27:16 -0500, Tim Daneliuk wrote:

> Given a program 'foo' that takes a command line argument '-I
> includefile', I want to be able to look for 'includefile' in a path
> specified in an environment variable, 'FOOPATH'.
> 
> I'd like a semantic that says:
> 
>   "If 'includefile' contains one or more path separator characters,
>ignore 'FOOPATH'. If it contains no path separators, look for it in
>the paths specified by 'FOOPATH', beginning with the leftmost path
>first."
> 
> Is there a standard Pythonic idiom for doing this or do I need to cook
> up my own.

There isn't an idiom.

There are a surprising number of choices for such a simple task, e.g.
whether the search path is used for relative paths containing a separator,
whether you stop at the first file which exists or the first file which
meets other criteria (e.g. suitable permissions), whether default
locations come first or last, what happens if a default location is
included in the search path, etc.

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