Re: Generators.

2009-12-07 Thread Lie Ryan

First, I apologize for rearranging your message out of order.

On 12/8/2009 5:29 AM, Jorge Cardona wrote:

islice execute the function at the generator and drop the elements
that aren't in the slice. I found that pretty weird, the way that i
see generators is like an association between and indexing set (an
iterator or another generator) and a computation that is made indexed
by the indexing set, and islice is like a "transformation" on the
indexing set,it doesn't matter the result of the function, the slice
should act only on the indexing set, some other "transformation" like
takewhile act on the result so, the execution it has to be made, but
in the islice, or other "transformation" that act only in the indexing
set, the function shouldn't be executed at each element, but only on
that new set that result of the application of the "transformation" on
the original set.


that seems like an extremely lazy evaluation, I don't know if even a true
lazy language do that. Python is a strict language, with a few laziness
provided by generators, in the end it's still a strict language.



Yes, it looks like lazy evaluation, but, i don't see why there is not
a better control over the iterable associated to a generator, even
with Python that is a strict language, it will increase the
functionality of it, and the performance too, imagine that you pass a
function that takes 1 sec in run, and for several reason you can't
slice before (as the smp function that i want to create), the final
performance with the actual islice it gets really reduced
Just creating the separation between those transformation that act on
the index(islice, or tee) on those that act on the result(dropwhile,
takewhile, etc.) the control could be fine enough to increase
usability (that's the way i think right now), and you will be able to
combine generator without lose performance.


Theoretically yes, but the semantic of generators in python is they work 
on an Iterable (i.e. objects that have __iter__), instead of a Sequence 
(i.e. objects that have __getitem__). That means semantically, 
generators would call obj.__iter__() and call the iter.__next__() and do 
its operation for each items returned by the iterator's iterable's 
__next__().


The lazy semantic would be hard to fit the current generator model 
without changing the semantics of generator to require a Sequence that 
supports indexing.



Yes, it looks like lazy evaluation, but, i don't see why there is not
a better control over the iterable associated to a generator, even
with Python that is a strict language


You can control the laziness by making it explicitly lazy:

from functools import partial
def f(x):
print("eval: %d"%x)
return x

X = range(10)
g = (partial(f, x) for x in X)

print(list(x() for x in islice(g,0,None,2)))
# # or without partial:
# g = ((lambda: f(x)) for x in X)
# print(list(f() for f in islice(g,0,None,2)))

In a default-strict language, you have to explicitly say if you want 
lazy execution.



What i want to do is a function that receive any kind of generator and
execute it in several cores (after a fork) and return the data, so, i
can't slice the set X before create the generator.


beware that a generator's contract is to return a valid iterator *once* 
only. You can use itertools.tee() to create more generators, but tee 
built a list of the results internally.

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


Re: Duplicates of third-party libraries

2009-12-07 Thread Martin P. Hellwig

Ben Finney wrote:


This omits the heart of the problem: There is an extra delay between
release and propagation of the security fix. When the third-party code
is released with a security fix, and is available in the operating
system, the duplicate in your application will not gain the advantage of
that fix until you release a new version of your application *and* that
new version makes its way onto the affected computer.

That is an additional delay, that only occurs because the hypothetical
security bug exists in a duplicate copy of the third party code. That
delay is entirely eradicated if your application instead uses a
system-installed library; your application then gains the security fix
immediately when they upgrade the system-installed library, without the
user having to upgrade the application at all.



I fully agree with your reasoning and I think you raised a valid point.

However, for me (as in YMMV), I have observed the following behaviour:
- Distribution security fixes are time wise at best on-par with my releases.
- Although some distribution (like ubuntu) offer updates on third party 
dependencies (like the Python interpreter), most of them don't (windows).
- A user is more likely to update a program he uses than a third party 
dependency he doesn't think he uses, especially if that program has an 
auto-update feature and the dependency doesn't.
- In the ideal world, a upgrade of a dependency won't break your 
program, in reality users fear upgrading dependencies because they don't 
know for sure it won't result in a dll hell type of problem.


With these observations in mind and that it gives me more control on 
what I am delivering, I made the trade off that I send platform specific 
 fully self contained (as far as possible) executables.


But you are right that it does give me the obligation to provide a way 
to the customer to get updates ASAP if there is a security issue in my 
program, whether this comes originally from a third party library or not 
is in the users perspective, rightfully so, beside the point.



--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Duplicates of third-party libraries

2009-12-07 Thread Ben Finney
"Martin P. Hellwig"  writes:

> Ben Finney wrote:
> > Along with the duplication this introduces, it also means that any bug
> > fixes — even severe security fixes — in the third-party code will not be
> > addressed in your duplicate. 

> I disagree, what you need is:
> - An automated build system for your deliveries, something you should
> have anyway
> - An method of tracking versions of your dependencies, again something
> you should have anyway

Those are orthogonal issues. They're good practice, but don't address
the problem I'm describing.

> - And a policy that you incorporate bug fixes from your dependencies
> in your deliveries, something you should do anyway if you are serious
> about your product.

This omits the heart of the problem: There is an extra delay between
release and propagation of the security fix. When the third-party code
is released with a security fix, and is available in the operating
system, the duplicate in your application will not gain the advantage of
that fix until you release a new version of your application *and* that
new version makes its way onto the affected computer.

That is an additional delay, that only occurs because the hypothetical
security bug exists in a duplicate copy of the third party code. That
delay is entirely eradicated if your application instead uses a
system-installed library; your application then gains the security fix
immediately when they upgrade the system-installed library, without the
user having to upgrade the application at all.

-- 
 \“I took it easy today. I just pretty much layed around in my |
  `\underwear all day. … Got kicked out of quite a few places, |
_o__)  though.” —Bug-Eyed Earl, _Red Meat_ |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Duplicates of third-party libraries

2009-12-07 Thread Martin P. Hellwig

Ben Finney wrote:

"Martin P. Hellwig"  writes:




Along with the duplication this introduces, it also means that any bug
fixes — even severe security fixes — in the third-party code will not be
addressed in your duplicate. 


I disagree, what you need is:
- An automated build system for your deliveries, something you should 
have anyway
- An method of tracking versions of your dependencies, again something 
you should have anyway
- And a policy that you incorporate bug fixes from your dependencies in 
your deliveries, something you should do anyway if you are serious about 
your product.


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Graph library for Python

2009-12-07 Thread geremy condra
On Mon, Dec 7, 2009 at 6:28 PM, M.-A. Lemburg  wrote:
> geremy condra wrote:
>> On Mon, Dec 7, 2009 at 2:51 PM, M.-A. Lemburg  wrote:
>>> geremy condra wrote:
 How interested are you in a C port of graphine? I haven't had
 any specific requests for it, but if its something you need I
 can shuffle it towards the top of the to do pile.
>>>
>>> There are two main reasons for a C implementation:
>>>
>>>  1. performance
>>>
>>>  2. memory footprint
>>>
>>> These are important when using graphs with lots of nodes or
>>> when using lots of graphs or operations on graphs in tight
>>> loops.
>>>
>>> However, to get such a new type into the core, you need to start
>>> a PEP process and hash out the API some more, esp. with respect
>>> to integration with the other core types, such as dictionaries
>>> and lists of tuples for both creation and as alternative
>>> representation.
>>
>> I'm happy to start the PEP process, but will need some
>> guidance, as I've never written a PEP before.
>
> Some pointers to get you started:
>
> PEPs in general:
> http://www.python.org/dev/peps/pep-0001/
> http://www.python.org/dev/peps/pep-0009/
>
> Adding modules to the standard lib:
> http://www.python.org/dev/peps/pep-0002/
>
> (though, in reality, you'd probably only be patching the
> collections.py module, I guess)

Thanks, I'll go over these a little later.

>>> Some other comments (in no particular order):
>>>
>>>  * the "name" default of using id(node) is not ideal, since
>>>   id(node) will return the address of the object and that
>>>   can be subject to reuse within the lifetime of the process;
>>>   using a global (thread-safe) counter would be safer
>>
>> Good idea. I'm going to start a branch on the repo to
>> handle the changes you mention.
>>
>>>  * Graph.__init__ should be able to take a list or set
>>>   of nodes and edges as initializer
>>
>> The format of this will need to be thought all the way
>> through before being implemented. To date, we haven't
>> come up with anything completely satisfactory, but
>> AFAIK everybody involved is still open to suggestions
>> on this.
>
> I wasn't thinking of anything clever :-) ...
>
> g = Graph(
>      [Node("a"), Node("b"), Node("c")],
>      [Edge(Node("a"), Node("b"), "ab"),
>       Edge(Node("a"), Node("c"), "ac"),
>       Edge(Node("b"), Node("c"), "bc"),
>      ])
>
> The main motivation here is to get lists, sets and dicts
> play nice together.

Generally, we've tried to discourage people from instantiating
nodes and edges directly, in favor of having them controlled
through the graph. Maybe something along the lines of:

g = Graph(nodes=['a', 'b', 'c'], edges=[('a', 'b'), ('a', 'c'), ('b', 'c')])

?

>>>  * Graph.__setitem__ could be mapped to .add_node() for
>>>   convenience
>>
>> This may be a question of playing around with it. ATM I'm
>> not sold on the idea, but I'll implement it and see how it
>> turns out in practice.
>
> Thinking about it some more, I agree, it's not all that useful.
>
>>>  * Graph.__length__ could be mapped to .size() for
>>>   convenience
>>
>> We decided not to do this due to the ambiguity between
>> whether .size() or .order() was the intended operation,
>> and looking back I'm not sure that was entirely unjustified.
>> Do you see there being any confusion on that score?
>
> There is an ambiguity here, indeed. My thinking was that
> the edges define the graph and can be mapped to a dictionary,
> e.g.
>
> d = {"ab": ("a", "b"),
>     "ac": ("a", "c"),
>     "bc": ("b", "c")}
>
> len(d) == 3
> len(g) == 3
>
> A graph without edges is also what you typically call an empty
> graph, ie.
>
> if not g:
>    print 'empty'
>
> http://mathworld.wolfram.com/EmptyGraph.html
>
> Still, perhaps it's better to just not go down this route.

I'd rather avoid it if possible, since there are many
potential interpretations of this in different contexts.
Again, I'm open to to the idea, but not convinced.

>>>  * Graph.__iter__ could be mapped to an iterator using
>>>   the fastest traversal method for the graph nodes
>>>   (ie. order does not matter, it's only important that
>>>   all nodes are found as fast as possible)
>>
>> Again, it seems ambiguous as to whether nodes or
>> edges are the intended target here, and while the
>> API can obviously dictate that, it seems a bit like
>> a case of "in the face of ambiguity, refuse the
>> temptation to guess" to me.
>
> Right, but sometimes "practicalty beats purity" ;-) We had
> the same situation for dictionaries and then decided that
> iteration over keys would be more natural than iterating over
> items (key, value) or values.
>
> It's also important to note that:
>
>        for n in g: print n
>
> and
>
>        n in g
>
> match up in terms of semantics.
>
> Since n in g already uses the "node in graph" approach,
> the for-loop should use the same logic.

Beware, that doesn't just match nodes.

g = Graph()
g.add_node('a')
g.add_node('b')
g.add_edge('a', 'b', 'ab')
'ab' in g # returns true

>>>  * G

Re: How decoupled are the Python frameworks?

2009-12-07 Thread J Kenneth King
shocks  writes:

> Hi
>
> I'm getting back into Python after a long break.  I've been developing
> large enterprise apps solely with Adobe Flex (ActionScript) for the
> past couple years.  During that time I've used a number of 'MVC'
> frameworks to glue the bits together - among them Cairngorm, a
> modified implementation of Cairngorm using the Presentation Model
> pattern, PureMVC, Mate (an IOC container but with an MVC
> implementation) and Parsley (IOC but you have to roll-you-own MVC).
> During that time I've been in large teams (30 Flex + 30 Java) to small
> teams (2 Flex + 1 Java).  The motivation of these frameworks is the
> decouple your concerns, allowing your apps to be more scalable, easier
> to test, and  supposedly easier to maintain.  Some do the decoupling
> better job than others, but there is also the question of "how
> decoupled is your code from the framework"?  It's all well and good
> having something clever working behind the scenes wiring and routing
> everything together, but I wonder where this leaves the code base if
> the framework, which was selected at the beginning of the project, is
> replaced with something else months or years later (i.e. the framework
> just doesn't scale as expected, the community involvement dies and
> it's no longer maintained properly, etc).  I've seen it happen and
> I've been experienced the pain of detangling massive amounts of code
> which is full of framework specific imports, methods and boilerplate
> code.  And then there's updating the unit tests!
>
> My question is how good are the current crop of Python frameworks?
> I've used Django twice in production and didn't like that much.  The
> implementation is Django specific for starters.  I've picked up Pylons
> and I'm trying that out.  I'm not sure how well it fares?  I do feel a
> bit uneasy about the code generation that some of the Python
> frameworks do.  Pylons creates something like 20 files for a
> 'helloworld'.  It does do some great things out of the box, but I
> wonder where that leaves your own code.  After spending 3-6 months on
> your Pylons webapp, how easy is it to move to something else?  Maybe
> one of the Python IOC once they mature.  What are some good techniques
> people are using to future (framework) proof their apps?
>
> I'm interested to hear people experiences with the various frameworks
> and how decoupled their code is from them.  The best of the current
> Flex frameworks for me is Parsley.  The only noticeable Parlsey code
> is an '[Inject]' meta tag here and there and a couple import
> statements.  All the complicated object creation and messaging is done
> higher up the chain.
>
> Cheers,
> Ben

I've stayed away from the Java world at least professionally... but I
think I understand where you're getting.

I'm not a huge fan of web development.  Which is rather
counter-intuitive for me to say since it's been the bulk of my work
for the past four years.  It's because there's no easy way to get
around the warts.  Websites are one thing but to try and think of
these things as "applications" becomes an expensive illusion to
maintain.

The problem with thinking about these things as applications with an
interface, a controller, and a model is: statelessness!  Oh also
serialization.  Getting around these issues are pretty much the raison
d'etre for web frameworks.  Without them we end up with PHP.

As far as swappable Python web frameworks... NONE. AFAIK, they all
require some commitment to tying your application to them.  However,
there are a crop of frameworks that do minimize the pain of such a
decision: web.py and bobo are two that I've been working with (though
it sounds like cherrypy would be very good at separating dispatching
from application code).  You could of course write your stuff closer
to the "metal" so to speak... WSGI would be about as low as I go.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to convert string function to string method?

2009-12-07 Thread Dr. Phillip M. Feldman

Bruno- You've made some excellent suggestions, and I'm always grateful for
the opportunity to learn.  My revised code appears below.  Philllip

def strip_pairs(s, open='([{\'"', close=')]}\'"'):
   """
   OVERVIEW

   This function strips matching pairs of characters from the beginning and
   end of the input string `s`.  If `s` begins with a character in `open`
and
   ends with the corresponding character in `close` (see below), both are
   removed from the string. This process continues until no further matching
   pairs can be removed.

   INPUTS

   `open` and `close`: These arguments, which must be equal-length strings,
   specify matching start-of-scope and end-of-scope characters.  The same
   character may appear in both `open` and `close`; single and double quotes
   conventionally match themselves.  By default, `open` contains a left
   parenthesis, left square bracket, left curly bracket, single quote, and
   double quote), and `close` contains the corresponding characters."""

   if not isinstance(s,(str,unicode)):
  raise TypeError, '`s` must be a string (str or unicode).'

   if not isinstance(open,(str,unicode)) or not
isinstance(close,(str,unicode)):
  raise TypeError, '`open` and `close` must be strings (str or
unicode).'

   if len(open) != len(close): raise ValueError, \
 '\'open\' and \'close\' arguments must be equal-length strings.'

   while len(s) >= 2:

  # Check whether first character of `s` is in `open`:
  i= open.find(s[0])

  # If `s` does not begin with a character from `open`, there are no
more
  # pairs to be stripped:
  if i == -1: break

  # If `s` does not begin and end with matching characters, there are no
  # more pairs to be stripped:
  if s[-1] != close[i]: break

  # Strip the first and last character from `s`:
  s= s[1:-1]

   return s
-- 
View this message in context: 
http://old.nabble.com/how-to-convert-string-function-to-string-method--tp26673209p26688035.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: test if an input string starts with a python expression

2009-12-07 Thread Richard Thomas
On Dec 8, 1:22 am, r0g  wrote:
> Torsten Mohr wrote:
> > Hi,
>
> > i'd like to test if an input string starts with a python expression
> > and also where that expression ends.  An example:
>
> > a_func(3*7, '''abc''') +5 pls some more
>
> > The first part until (inclusive) the 5 should be found as an expression
> > and the length of that string should also be detected.
>
> > Background is that i want to embed some python expressions in a text
> > and i want to evaluate them later.
> > It is possible that the embedded expressions span several lines.
>
> > Alternatively, is it possible to define a start- and an end-marker
> > that define the embedded expression and find the expression using
> > a regular expression?
>
> That's the easy way and will work for most cases if you use uncommon
> delimiters. I tend to use '<<<' and '>>>' for things like this but you
> can make them as obscure as you like.
>
> > If the expression contains strings, these strings could contain
> > the end-marker which should not be found inside strings.
>
> You could build in escaping but that complicates things quite quickly,
> assuming this is for your own private use and you won't be dealing with
> huge rafts of data from elsewhere or using this to control radiotherapy
> machines etc, that's probably overkill.
>
> The re module should do everything you need and is part of the standard lib.
>
> >>> import re
> >>> regex = re.compile(r'<<<(.{1,500}?)>>>', re.DOTALL)
> >>> regex.findall("the cat <<>> on the <<>>")
>
> ['sat', 'mat']
>
> Hope this helps,
>
> Roger.

Use the parser module.

>>> parser.expr('func(a+1)')


>>> parser.expr('print a')
...
SyntaxError: invalid syntax

Quite intensive as you have to compile every initial substring to find
the longest one but at least you use Python's own definition of an
expression.

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


Request for py program to insert space between two characters and saved as text?

2009-12-07 Thread 74yrs old
For Kannada project .txt(not .doc) is used, my requirement is to have one
space between two characters in Notepad file.  In MSword there is provision
to make space between two characters under "Font" and  can be saved as *.doc
*  But when tried to save as* .txt*  all formatting will disappear. I could
not understand how to do in notepad. Even tried copy and paste from doc to
notepad but failed.

In this context, I request you kindly for small python program - to make or
insert space between two characters in the text file. (I have installed
FFedora-11 also ubuntu9.04)

example: *F o r  K a n n a d a  p r o j e c t . t x t(n o t .d o c)  i s  u
s e d,  m y  r e q u i r e m e n t   i s  t o  h a v e  o n e  s p a c e  b
e t w e e n
 t w o  c h a r a c t e r s  i n  t h e  t e x t.*
 *ಕ ನ್ನ ಡ ವ ನ್ನು  ಕ ಲಿ ಯಿ ರೀ *
-- 
http://mail.python.org/mailman/listinfo/python-list


Request for solution

2009-12-07 Thread 74yrs old
Python experts,

Please find attached file "Tesseractindic-Trainer-GUI-0-
1-3.tar.gz for perusal.("if you like to help, I can send you the program
off-list") The said tesseractindic-trainer works well in Linux viz
Ubuntu-9.04 and fedora-11 without any problem.

But when I tried to run in CMD.exe of WinXP it fails. In WinXP, I have
already installed Python2.6 and python3.0 as well as GTK under C:\drive.
When run c:\GTK> gtk-demo it will open "ApplicationMainwindow."

Is it possible to test the said trainer and modified relevant source code
accordingly,  to run in WinXP successfully - for which I shall be thankful
to you.
Since I am newbie to python, I am seeking  valuable guidance as special
case.
With regards,
-sriranga(77yrsold)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generators.

2009-12-07 Thread Taylor
On Dec 7, 1:29 pm, Jorge Cardona  wrote:
> 2009/12/7 Lie Ryan :
>
>
>
> > On 12/7/2009 7:22 AM, Jorge Cardona wrote:
>
> >> Hi,
>
> >> I was trying to create a function that receive a generator and return
> >> a list but that each elements were computed in a diferent core of my
> >> machine. I start using islice function in order to split the job in a
> >> way that if there is "n" cores each "i" core will compute the elements
> >> i,i+n,i+2n,..., but islice has a weird (to me) behavior, look:
>
> > it's nothing weird, python just do what you're telling it to:
>
> > transform all x in X with f(x)
>
> >> g = (f(x) for x in X)
>
> > then slice the result of that
>
> >> print(list(x for x in islice(g,0,None,2)))
>
> When i wrote that first line of code i thought that i was creating a
> generator that will later compute  the elements of X with function f,
> if i will like to transform them immediately i would use [] instead
> (), so, the result is not where i want to slice, even so, is exactly
> the same to slice, after or before, the only difference is that after
> the compute i'm losing 5 in unnecessary execution of the function f.
>
> > what you want to do is to slice before you transform:
>  g = (x for x in islice(X, 0, None, 2))
>  print(list(f(x) for x in g))
> > eval: 0
> > eval: 2
> > eval: 4
> > eval: 6
> > eval: 8
> > [0, 2, 4, 6, 8]
>
> What i want to do is a function that receive any kind of generator and
> execute it in several cores (after a fork) and return the data, so, i
> can't slice the set X before create the generator.
>
>
>
> >> islice execute the function at the generator and drop the elements
> >> that aren't in the slice. I found that pretty weird, the way that i
> >> see generators is like an association between and indexing set (an
> >> iterator or another generator) and a computation that is made indexed
> >> by the indexing set, and islice is like a "transformation" on the
> >> indexing set,it doesn't matter the result of the function, the slice
> >> should act only on the indexing set, some other "transformation" like
> >> takewhile act on the result so, the execution it has to be made, but
> >> in the islice, or other "transformation" that act only in the indexing
> >> set, the function shouldn't be executed at each element, but only on
> >> that new set that result of the application of the "transformation" on
> >> the original set.
>
> > that seems like an extremely lazy evaluation, I don't know if even a true
> > lazy language do that. Python is a strict language, with a few laziness
> > provided by generators, in the end it's still a strict language.
>
> Yes, it looks like lazy evaluation, but, i don't see why there is not
> a better control over the iterable associated to a generator, even
> with Python that is a strict language, it will increase the
> functionality of it, and the performance too, imagine that you pass a
> function that takes 1 sec in run, and for several reason you can't
> slice before (as the smp function that i want to create), the final
> performance with the actual islice it gets really reduced
> Just creating the separation between those transformation that act on
> the index(islice, or tee) on those that act on the result(dropwhile,
> takewhile, etc.) the control could be fine enough to increase
> usability (that's the way i think right now), and you will be able to
> combine generator without lose performance.
>
> >> Well, it works for what i need, but is not very neat, and i think that
> >> there it should be a formal way to act on the base indexing iterator,
> >> such way exists? Is there a better approach to get what i need?
>
> > Reverse your operation.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> --
> Jorge Eduardo Cardona
> jorgeecard...@gmail.com
> jorgeecardona.blogspot.com
> 
> Linux registered user  #391186
> Registered machine    #291871
> 

What would you have your islice do for the following generator?

def fib(n):
a,b=0,1
for x in range(n):
a,b=b,a+b
yield a

In order for some value it yields to be correct, it needs execute all
the previous times. If it happens that some of the results aren't
used, they are thrown out. As is, using your islice (on, say, fib(10))
gives a KeyError for the key '.0'.
Point is, generators don't work that way. You aren't guaranteed to be
able to jump around, only to find the next value. If you need to split
a particular generator up into parts that can be computed separately,
your best bet is probably to rewrite that generator.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test if an input string starts with a python expression

2009-12-07 Thread r0g
Torsten Mohr wrote:
> Hi,
> 
> i'd like to test if an input string starts with a python expression
> and also where that expression ends.  An example:
> 
> a_func(3*7, '''abc''') +5 pls some more
> 
> The first part until (inclusive) the 5 should be found as an expression
> and the length of that string should also be detected.
> 
> 
> Background is that i want to embed some python expressions in a text
> and i want to evaluate them later.
> It is possible that the embedded expressions span several lines.
> 
> Alternatively, is it possible to define a start- and an end-marker
> that define the embedded expression and find the expression using
> a regular expression?


That's the easy way and will work for most cases if you use uncommon
delimiters. I tend to use '<<<' and '>>>' for things like this but you
can make them as obscure as you like.



> If the expression contains strings, these strings could contain
> the end-marker which should not be found inside strings.


You could build in escaping but that complicates things quite quickly,
assuming this is for your own private use and you won't be dealing with
huge rafts of data from elsewhere or using this to control radiotherapy
machines etc, that's probably overkill.


The re module should do everything you need and is part of the standard lib.


>>> import re
>>> regex = re.compile(r'<<<(.{1,500}?)>>>', re.DOTALL)
>>> regex.findall("the cat <<>> on the <<>>")
['sat', 'mat']

Hope this helps,

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


Fwd: Request for solution

2009-12-07 Thread 74yrs old
-- Forwarded message --
From: Sjoerd Mullender 
Date: Mon, Dec 7, 2009 at 11:57 PM
Subject: Re: Request for solution
To: 74yrs old 
Cc: python-list-ow...@python.org


Please send this to python-list@python.org, not to
python-list-ow...@python.org.  The latter address is only to send
messages to the administrator of the mailing list.

On 2009-12-07 17:09, 74yrs old wrote:
> Hello experts,
> For Kannada project .txt(not .doc) is used, my requirement is to have
> one space between two characters in Notepad file.  In MSword there is
> provision to make space between two characters under "Font" and  can be
> saved as _.doc_  But when tried to save as_ .txt_  all formatting will
> disappear. I could not understand how to do in notepad. Even tried copy
> and paste from doc to notepad but failed.
>
> In this context, I request you kindly for small Python program - to make
> or insert space between two characters in the _text_ file. (I have
> installed Fedora-11 and also ubuntu9.04)
>
> example: *F o r  K a n n a d a  p r o j e c t . t x t(n o t .d o c)  i
> s  u s e d,  m y  r e q u i r e m e n t   i s  t o  h a v e  o n e  s p
> a c e  b e t w e e n  t w o  c h a r a c t e r s  i n  t h e  t e x t.*
>  *ಕ ನ್ನ ಡ ವ ನ್ನು  ಕ ಲಿ ಯಿ ರೀ
> With regards,
> -sriranga(77yrsold)
> *


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


Re: question about imports in a class

2009-12-07 Thread r0g
Diez B. Roggisch wrote:
> J schrieb:
>> On Mon, Dec 7, 2009 at 16:57, Diez B. Roggisch 
>> wrote:
>>
 if I put the import at the beginning of the class, it just dawned on
 me that perhaps I still have to explicitly call the function by class:


> I'm not annoyed. I just wonder from what you got the impression that
> import statements are something that belongs inside functions - even if
> python allows it.
> 
> I don't know of any other language (I don't do ruby & perl) that allows
> this, C/C++, java, Pascal, Haskell, ML - all of them declare their
> intended use of a library on the beginning of a file.
> 
> You seem to really be really determined to place them everywhere else...
> 
> 
> 
> 
> Diez


I have found one good use for using import within functions.

I maintain a module of useful miscellaneous python functions I have
written over the years, it's quite the hodgepodge though and it's not
something I would really want to package and distribute in its entirety,
especially as there are many uses of modules outside the standard lib.

I use it 'as is' during initial development with each function importing
what it needs at the top of the function definition. This doesn't seem
to affect performance and even if it did it would only be on my dev box.

When it's time to bundle an app for distribution I copy the few
functions that are actually used to a new module and refactor it so the
imports are all at the top. Having the imports within the functions
makes it very easy to keep track of these dependencies so I don't have
to sift through a list of 50 different imports, weeding out the ones I
don't need whenever I do this.

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


Re: test if an input string starts with a python expression

2009-12-07 Thread zeph
It sort of sounds like you want a templating system:
http://wiki.python.org/moin/Templating
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python proxy checker ,change to threaded version

2009-12-07 Thread r0g
Rhodri James wrote:
> On Mon, 07 Dec 2009 18:21:23 -, Terry Reedy  wrote:
> 
>> r0g wrote:
>>
>>> The trick to threads is to create a subclass of threading.Thread, define
>>> the 'run' function and call the 'start()' method. I find threading quite
>>> generally useful so I created this simple generic function for running
>>> things in threads...
>>
>> Great idea. Thanks for posting this.
>>
>>> def run_in_thread( func, func_args=[], callback=None,
>>> callback_args=[] ):
> 
> I'm might wary of having mutable defaults for parameters.  They make for
> the most annoying errors.  Even though they're actually safe here, I'd
> still write:
> 
> def run_in_thread(func, func_args=(), callback=None, callback_args=()):
> 
> out of sheer paranoia.
> 
>



Excellent point, thanks :)

I'm starting to suspect this is the highest quality group in all of usenet!


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


Re: python proxy checker ,change to threaded version

2009-12-07 Thread r0g
Terry Reedy wrote:
> r0g wrote:
> 
>> The trick to threads is to create a subclass of threading.Thread, define
>> the 'run' function and call the 'start()' method. I find threading quite
>> generally useful so I created this simple generic function for running
>> things in threads...
> 
> Great idea. Thanks for posting this.
> 
>> def run_in_thread( func, func_args=[], callback=None, callback_args=[] ):



Okay, so here's the more concise version for posterity / future googlers...

import threading

def run_in_thread( func, func_args=[], callback=None, callback_args=[] ):
class MyThread ( threading.Thread ):
def run ( self ):
result = func(*func_args)
if callback:
callback(result, *callback_args)
MyThread().start()


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


test if an input string starts with a python expression

2009-12-07 Thread Torsten Mohr
Hi,

i'd like to test if an input string starts with a python expression
and also where that expression ends.  An example:

a_func(3*7, '''abc''') +5 pls some more

The first part until (inclusive) the 5 should be found as an expression
and the length of that string should also be detected.


Background is that i want to embed some python expressions in a text
and i want to evaluate them later.
It is possible that the embedded expressions span several lines.

Alternatively, is it possible to define a start- and an end-marker
that define the embedded expression and find the expression using
a regular expression?
If the expression contains strings, these strings could contain
the end-marker which should not be found inside strings.

Example:

start: <
end:   >

< some_func(r">")>


Can anybody tell me how to do this best?

Can i do this using just modules from the python library?


Thanks for any hints,
Torsten.

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


Re: python proxy checker ,change to threaded version

2009-12-07 Thread r0g
Terry Reedy wrote:
> r0g wrote:
> 
>> The trick to threads is to create a subclass of threading.Thread, define
>> the 'run' function and call the 'start()' method. I find threading quite
>> generally useful so I created this simple generic function for running
>> things in threads...
> 
> Great idea. Thanks for posting this.
> 
>> def run_in_thread( func, func_args=[], callback=None, callback_args=[] ):
>> import threading
>> class MyThread ( threading.Thread ):
>>def run ( self ):
>>
>> # Call function
>> if function_args:
>> result = function(*function_args)
>> else:
>> result = function()
> 
> The check is not necessary. by design, f(*[]) == f()


Excellent, thanks Terry :)

I knew it would be simpler than I thought! I've been writing a lot of
PHP and AS3 recently and it's easy to forget how python often just works
without needing the same level of hand holding, error checking &
defensive coding as other languages!



> Names do not match param names ;=)


Oops yeah! Thought I'd refactor my painfully verbose variable names
before posting in a 70 char wide medium but it looks like I missed one!
*blush*

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


Re: Graph library for Python

2009-12-07 Thread M.-A. Lemburg
geremy condra wrote:
> On Mon, Dec 7, 2009 at 2:51 PM, M.-A. Lemburg  wrote:
>> geremy condra wrote:
>>> How interested are you in a C port of graphine? I haven't had
>>> any specific requests for it, but if its something you need I
>>> can shuffle it towards the top of the to do pile.
>>
>> There are two main reasons for a C implementation:
>>
>>  1. performance
>>
>>  2. memory footprint
>>
>> These are important when using graphs with lots of nodes or
>> when using lots of graphs or operations on graphs in tight
>> loops.
>>
>> However, to get such a new type into the core, you need to start
>> a PEP process and hash out the API some more, esp. with respect
>> to integration with the other core types, such as dictionaries
>> and lists of tuples for both creation and as alternative
>> representation.
> 
> I'm happy to start the PEP process, but will need some
> guidance, as I've never written a PEP before.

Some pointers to get you started:

PEPs in general:
http://www.python.org/dev/peps/pep-0001/
http://www.python.org/dev/peps/pep-0009/

Adding modules to the standard lib:
http://www.python.org/dev/peps/pep-0002/

(though, in reality, you'd probably only be patching the
collections.py module, I guess)

>> Some other comments (in no particular order):
>>
>>  * the "name" default of using id(node) is not ideal, since
>>   id(node) will return the address of the object and that
>>   can be subject to reuse within the lifetime of the process;
>>   using a global (thread-safe) counter would be safer
> 
> Good idea. I'm going to start a branch on the repo to
> handle the changes you mention.
> 
>>  * Graph.__init__ should be able to take a list or set
>>   of nodes and edges as initializer
> 
> The format of this will need to be thought all the way
> through before being implemented. To date, we haven't
> come up with anything completely satisfactory, but
> AFAIK everybody involved is still open to suggestions
> on this.

I wasn't thinking of anything clever :-) ...

g = Graph(
  [Node("a"), Node("b"), Node("c")],
  [Edge(Node("a"), Node("b"), "ab"),
   Edge(Node("a"), Node("c"), "ac"),
   Edge(Node("b"), Node("c"), "bc"),
  ])

The main motivation here is to get lists, sets and dicts
play nice together.

>>  * Graph.__setitem__ could be mapped to .add_node() for
>>   convenience
> 
> This may be a question of playing around with it. ATM I'm
> not sold on the idea, but I'll implement it and see how it
> turns out in practice.

Thinking about it some more, I agree, it's not all that useful.

>>  * Graph.__length__ could be mapped to .size() for
>>   convenience
> 
> We decided not to do this due to the ambiguity between
> whether .size() or .order() was the intended operation,
> and looking back I'm not sure that was entirely unjustified.
> Do you see there being any confusion on that score?

There is an ambiguity here, indeed. My thinking was that
the edges define the graph and can be mapped to a dictionary,
e.g.

d = {"ab": ("a", "b"),
 "ac": ("a", "c"),
 "bc": ("b", "c")}

len(d) == 3
len(g) == 3

A graph without edges is also what you typically call an empty
graph, ie.

if not g:
print 'empty'

http://mathworld.wolfram.com/EmptyGraph.html

Still, perhaps it's better to just not go down this route.

>>  * Graph.__iter__ could be mapped to an iterator using
>>   the fastest traversal method for the graph nodes
>>   (ie. order does not matter, it's only important that
>>   all nodes are found as fast as possible)
> 
> Again, it seems ambiguous as to whether nodes or
> edges are the intended target here, and while the
> API can obviously dictate that, it seems a bit like
> a case of "in the face of ambiguity, refuse the
> temptation to guess" to me.

Right, but sometimes "practicalty beats purity" ;-) We had
the same situation for dictionaries and then decided that
iteration over keys would be more natural than iterating over
items (key, value) or values.

It's also important to note that:

for n in g: print n

and

n in g

match up in terms of semantics.

Since n in g already uses the "node in graph" approach,
the for-loop should use the same logic.

>>  * Graph could also benefit from some bulk update methods,
>>   e.g. to update weights on all edges or nodes by passing
>>   in a dictionary mapping names to attribute values
> 
> Sounds good. Again, the format will need some careful
> thought, but you're right that this would improve it
> greatly.

This is only an optimization, but could lead to some great
performance improvements by avoiding function call overhead.

>>  * Graph could benefit from some bulk query methods,
>>   such as .get_node_attributes() and .add_edges().
> 
> I'm not sure how the first is different from the existing
> .data, what did you have in mind?

The second one was a typo. It should have been
.get_edge_attributes().

In both cases I was thinking of being able to quickly
extract a number of node/edge attributes, e.g.

>>> g.get_edge_at

Duplicates of third-party libraries (was: When will Python 3 be fully deployed)

2009-12-07 Thread Ben Finney
"Martin P. Hellwig"  writes:

> If the fear of customers disatification prevents you from using a
> certain version of X, you should consider a deployment strategy that
> cuts out dependencies as much as possible. Although this will result
> in a larger end package and possible high amount of duplication, it is
> still preferable to just stop supporting popular platforms or be
> swamped away with bugs due to version mismatches.

Along with the duplication this introduces, it also means that any bug
fixes — even severe security fixes — in the third-party code will not be
addressed in your duplicate. This defeats one of the many benefits of a
package management operating system: that libraries, updated once, will
benefit any other package depending on them.

Please reconsider policies like including duplicates of third-party
code. Don't Repeat Yourself is a good principle not just within source
code, but has important security implications within the operating
system packages too.

-- 
 \ “Our task must be to free ourselves from our prison by widening |
  `\our circle of compassion to embrace all humanity and the whole |
_o__)   of nature in its beauty.” —Albert Einstein |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Graph library for Python (was: Re: python bijection)

2009-12-07 Thread geremy condra
On Mon, Dec 7, 2009 at 5:48 PM, Steven D'Aprano
 wrote:
> On Mon, 07 Dec 2009 17:23:24 -0500, geremy condra wrote:
>
>
>>>  * Graph.__iter__ could be mapped to an iterator using
>>>   the fastest traversal method for the graph nodes (ie. order does not
>>>   matter, it's only important that all nodes are found as fast as
>>>   possible)
>>
>> Again, it seems ambiguous as to whether nodes or edges are the intended
>> target here, and while the API can obviously dictate that, it seems a
>> bit like a case of "in the face of ambiguity, refuse the temptation to
>> guess" to me.
>
> Consider dicts. They have methods that iterates over keys, values, and
> (key, value) pairs, but if you just iterate over the dict itself, you get
> the keys. This was a deliberate design decision.
>
> It's not a matter of guessing in the face of ambiguity, but giving an
> easy way to get the most common case. Assuming that iteration over nodes
> is more common than iteration over edges, then iter(graph) should yield
> nodes in some unspecified order. If you need the nodes in a particular
> order, then you will call some method that guarantees the order, and if
> you want the edges, you call a method that gives edges.
>
> If you're not sure whether wanting nodes or edges will be more common,
> then you should wait until you, or the community, has more real-world
> experience with the module. Python had dicts for something approaching a
> decade before they became directly iterable.

I don't have a problem with adding this if there's a strong desire for it,
but at the moment I'm leaning towards a wait-and-see approach, for
all the reasons you described.

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


Re: Graph library for Python (was: Re: python bijection)

2009-12-07 Thread Steven D'Aprano
On Mon, 07 Dec 2009 17:23:24 -0500, geremy condra wrote:


>>  * Graph.__iter__ could be mapped to an iterator using
>>   the fastest traversal method for the graph nodes (ie. order does not
>>   matter, it's only important that all nodes are found as fast as
>>   possible)
> 
> Again, it seems ambiguous as to whether nodes or edges are the intended
> target here, and while the API can obviously dictate that, it seems a
> bit like a case of "in the face of ambiguity, refuse the temptation to
> guess" to me.

Consider dicts. They have methods that iterates over keys, values, and 
(key, value) pairs, but if you just iterate over the dict itself, you get 
the keys. This was a deliberate design decision.

It's not a matter of guessing in the face of ambiguity, but giving an 
easy way to get the most common case. Assuming that iteration over nodes 
is more common than iteration over edges, then iter(graph) should yield 
nodes in some unspecified order. If you need the nodes in a particular 
order, then you will call some method that guarantees the order, and if 
you want the edges, you call a method that gives edges.

If you're not sure whether wanting nodes or edges will be more common, 
then you should wait until you, or the community, has more real-world 
experience with the module. Python had dicts for something approaching a 
decade before they became directly iterable.



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


Re: Where is my namespace?

2009-12-07 Thread Simon Brunning
2009/12/7 Steven D'Aprano :
> On Mon, 07 Dec 2009 16:25:39 +, Simon Brunning wrote:
>> If you do "from blah import" the imported module itself isn't bound to
>> any name in the importing module - you can't get at it at all.
>
> Not quite -- you can get to it if you're willing to do some more work.

"A little inaccuracy sometimes saves tons of explanation." - Saki, The
Square Egg, 1924

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


Re: question about imports in a class

2009-12-07 Thread Diez B. Roggisch

J schrieb:

On Mon, Dec 7, 2009 at 16:57, Diez B. Roggisch  wrote:


if I put the import at the beginning of the class, it just dawned on
me that perhaps I still have to explicitly call the function by class:

sysinfo.py

class myclass():
   import os
   def findDMIDecode(self):
   for r,d,f in myclass.os.walk(args)

is that correct?

It is correct in the sense that it works, but it is by no means good
practice.


So good practice (I'm doing this for real as well as for my own
education) would be:

sysinfo.py:

import os

class ClassA():
sysinfo.os.walk()

class ClassB():
sysinfo.os.walk()


No, no sysinfo. Just


import os

class A():

   def foo(self):
   os.path.walk("/")



Sorry for the incredibly entry-level questions...  I'm a more senior
member on a few Linux related lists/groups, and I can certainly
understand if questions as basic as these are annoying, so I'm really
trying to not ask them unless I'm just really confused about
something.

OTOH, if there's something more akin to a python newbies kind of list
where this kind of basic thing is more appropriate, please let me
know, because really, I don't want to clutter up python-list with
stuff that would be better off elsewhere.


I'm not annoyed. I just wonder from what you got the impression that 
import statements are something that belongs inside functions - even if 
python allows it.


I don't know of any other language (I don't do ruby & perl) that allows 
this, C/C++, java, Pascal, Haskell, ML - all of them declare their 
intended use of a library on the beginning of a file.


You seem to really be really determined to place them everywhere else...




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


Re: Float precision and float equality

2009-12-07 Thread dbd
On Dec 7, 4:28 am, sturlamolden  wrote:
> ...
>
> You don't understand this at all do you?
>
> If you have a sine wave with an amplitude less than the truncation
> error, it will always be approximately equal to zero.
>
> Numerical maths is about approximations, not symbolic equalities.
>
> > 1.0 + eps is the smallest value greater than 1.0, distinguishable from
> > 1.0.
>
> Which is the reason 0.5*eps*sin(x) is never distinguishable from 0.
> ...

A calculated value of 0.5*eps*sin(x) has a truncation error on the
order of eps squared. 0.5*eps and 0.495*eps are readily distinguished
(well, at least for values of eps << 0.01 :).

At least one of us doesn't understand floating point.

Dale B. Dalrymple

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


Re: Float precision and float equality

2009-12-07 Thread sturlamolden
On 7 Des, 06:43, dbd  wrote:

> If you have
> samples of a sine wave with peak amplitude of one half eps, the "abs(x-
> y) < eps" test would report all values on the sine wave as equal to
> zero. This would not be correct.

You don't understand this at all do you?

If you have a sine wave with an amplitude less than the truncation
error, it will always be approximately equal to zero.

Numerical maths is about approximations, not symbolic equalities.


> 1.0 + eps is the smallest value greater than 1.0, distinguishable from
> 1.0.

Which is the reason 0.5*eps*sin(x) is never distinguishable from 0.


> A constant comparison value is not appropriate.

That require domain specific knowledge. Sometimes we look at a
significant number of digits; sometimes we look at a fixed number of
decimals; sometimes we look at abs(y/x). But there will always be a
truncation error of some sort, and differences less than that is never
significant.






>
> Mark was right, DaveA's discussion explains a strategy to use.
>
> Dale B. Dalrymple

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


Re: Float precision and float equality

2009-12-07 Thread Carl Banks
On Dec 7, 10:53 am, dbd  wrote:
> On Dec 7, 4:28 am, sturlamolden  wrote:
>
> > ...
>
> > You don't understand this at all do you?
>
> > If you have a sine wave with an amplitude less than the truncation
> > error, it will always be approximately equal to zero.
>
> > Numerical maths is about approximations, not symbolic equalities.
>
> > > 1.0 + eps is the smallest value greater than 1.0, distinguishable from
> > > 1.0.
>
> > Which is the reason 0.5*eps*sin(x) is never distinguishable from 0.
> > ...
>
> A calculated value of 0.5*eps*sin(x) has a truncation error on the
> order of eps squared. 0.5*eps and 0.495*eps are readily distinguished
> (well, at least for values of eps << 0.01 :).
>
> At least one of us doesn't understand floating point.

You're talking about machine epsilon?  I think everyone else here is
talking about a number that is small relative to the expected smallest
scale of the calculation.


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


How decoupled are the Python frameworks?

2009-12-07 Thread shocks
Hi

I'm getting back into Python after a long break.  I've been developing
large enterprise apps solely with Adobe Flex (ActionScript) for the
past couple years.  During that time I've used a number of 'MVC'
frameworks to glue the bits together - among them Cairngorm, a
modified implementation of Cairngorm using the Presentation Model
pattern, PureMVC, Mate (an IOC container but with an MVC
implementation) and Parsley (IOC but you have to roll-you-own MVC).
During that time I've been in large teams (30 Flex + 30 Java) to small
teams (2 Flex + 1 Java).  The motivation of these frameworks is the
decouple your concerns, allowing your apps to be more scalable, easier
to test, and  supposedly easier to maintain.  Some do the decoupling
better job than others, but there is also the question of "how
decoupled is your code from the framework"?  It's all well and good
having something clever working behind the scenes wiring and routing
everything together, but I wonder where this leaves the code base if
the framework, which was selected at the beginning of the project, is
replaced with something else months or years later (i.e. the framework
just doesn't scale as expected, the community involvement dies and
it's no longer maintained properly, etc).  I've seen it happen and
I've been experienced the pain of detangling massive amounts of code
which is full of framework specific imports, methods and boilerplate
code.  And then there's updating the unit tests!

My question is how good are the current crop of Python frameworks?
I've used Django twice in production and didn't like that much.  The
implementation is Django specific for starters.  I've picked up Pylons
and I'm trying that out.  I'm not sure how well it fares?  I do feel a
bit uneasy about the code generation that some of the Python
frameworks do.  Pylons creates something like 20 files for a
'helloworld'.  It does do some great things out of the box, but I
wonder where that leaves your own code.  After spending 3-6 months on
your Pylons webapp, how easy is it to move to something else?  Maybe
one of the Python IOC once they mature.  What are some good techniques
people are using to future (framework) proof their apps?

I'm interested to hear people experiences with the various frameworks
and how decoupled their code is from them.  The best of the current
Flex frameworks for me is Parsley.  The only noticeable Parlsey code
is an '[Inject]' meta tag here and there and a couple import
statements.  All the complicated object creation and messaging is done
higher up the chain.

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


Re: question about imports in a class

2009-12-07 Thread Steven D'Aprano
On Mon, 07 Dec 2009 16:53:46 -0500, J wrote:

> A little more education and playing around and I'm still not quite sure
> how to do this...
> 
> for the class i'm writing, I want to import os, sys and wmi globally for
> the class...

The best advice is Do Not Do It That Way. Just do your imports at the top 
level of the module:

import os

class MyClass():
def findDMIDecode(self):
for r,d,f in os.walk(args):
...

(Aside: it is traditional to write class names in Python using initial 
capitals.)


> if I put the import at the beginning of the class, it just dawned on me
> that perhaps I still have to explicitly call the function by class:
> 
> class myclass():
> import os
> def findDMIDecode(self):
> for r,d,f in myclass.os.walk(args)
> 
> is that correct?

As Diez says, that works but it's not recommended. It's considered bad 
form.

It may help if you understand what import does: it does two basic 
functions: it loads a module, and it creates a name in the current 
namespace. So when you put:

import os

in the module, it loads os and creates a name 'os' that is now available 
to everything in the current module without qualification. But when you 
do this:

class MyClass():
import os
...

the name 'os' is placed in the MyClass namespace, not the global 
namespace. So it is only available inside the MyClass namespace. That is 
precisely the same as if you had done this:

class MyClass():
os = "something goes here"
...

Having done that, you have created a class attribute named 'os', so in 
your methods you have to do this:

class MyClass():
import os
def findDMIDecode(self):
for r,d,f in self.os.walk(args):
...

This isn't exactly wrong, but it is terribly unusual and almost certainly 
unnecessary. The only reason I'd do it that way would be if:

(1) I needed to hide the os name from other classes or functions in the 
module; or 

(2) I wanted the ability to replace the os module in MyClass with a 
different module at runtime, without affecting other classes or functions.

E.g.:

inst = MyClass()
inst.os = my_fake_os_module
inst.findDMIDecode()  # calls my_fake_os_module.walk instead of os.walk

Both of these use-cases are very, very unusual. (I've never done needed 
to do this in real code.) Unless you have one of those rare cases, stick 
to the standard idiom of importing the module at the top level of the 
class. If you're not sure whether or not you need this, you almost 
certainly don't.


> How about if I have two classes in sysinfo.py and want to import os
> globally for BOTH classes to use? Same thing?

If one class inherits from the other, then self.os will work via the 
normal inheritance mechanism.

If they are independent classes, then you will need to import os in both 
of them, or just import it at the module level.


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


Re: question about imports in a class

2009-12-07 Thread Rhodri James

On Mon, 07 Dec 2009 22:18:49 -, J  wrote:

On Mon, Dec 7, 2009 at 16:57, Diez B. Roggisch   
wrote:



if I put the import at the beginning of the class, it just dawned on
me that perhaps I still have to explicitly call the function by class:

sysinfo.py

class myclass():
   import os
   def findDMIDecode(self):
   for r,d,f in myclass.os.walk(args)

is that correct?


It is correct in the sense that it works, but it is by no means good
practice.


So good practice (I'm doing this for real as well as for my own
education) would be:

sysinfo.py:

import os

class ClassA():
sysinfo.os.walk()

class ClassB():
sysinfo.os.walk()


Nope.  Good practice would be:

import os

class ClassA(object):
os.walk()

class ClassB(object):
os.walk()

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


Re: Graph library for Python (was: Re: python bijection)

2009-12-07 Thread geremy condra
On Mon, Dec 7, 2009 at 2:51 PM, M.-A. Lemburg  wrote:
> geremy condra wrote:
>> How interested are you in a C port of graphine? I haven't had
>> any specific requests for it, but if its something you need I
>> can shuffle it towards the top of the to do pile.
>
> There are two main reasons for a C implementation:
>
>  1. performance
>
>  2. memory footprint
>
> These are important when using graphs with lots of nodes or
> when using lots of graphs or operations on graphs in tight
> loops.
>
> However, to get such a new type into the core, you need to start
> a PEP process and hash out the API some more, esp. with respect
> to integration with the other core types, such as dictionaries
> and lists of tuples for both creation and as alternative
> representation.

I'm happy to start the PEP process, but will need some
guidance, as I've never written a PEP before.

> Some other comments (in no particular order):
>
>  * the "name" default of using id(node) is not ideal, since
>   id(node) will return the address of the object and that
>   can be subject to reuse within the lifetime of the process;
>   using a global (thread-safe) counter would be safer

Good idea. I'm going to start a branch on the repo to
handle the changes you mention.

>  * Graph.__init__ should be able to take a list or set
>   of nodes and edges as initializer

The format of this will need to be thought all the way
through before being implemented. To date, we haven't
come up with anything completely satisfactory, but
AFAIK everybody involved is still open to suggestions
on this.

>  * Graph.__setitem__ could be mapped to .add_node() for
>   convenience

This may be a question of playing around with it. ATM I'm
not sold on the idea, but I'll implement it and see how it
turns out in practice.

>  * Graph.__length__ could be mapped to .size() for
>   convenience

We decided not to do this due to the ambiguity between
whether .size() or .order() was the intended operation,
and looking back I'm not sure that was entirely unjustified.
Do you see there being any confusion on that score?

>  * Graph.__iter__ could be mapped to an iterator using
>   the fastest traversal method for the graph nodes
>   (ie. order does not matter, it's only important that
>   all nodes are found as fast as possible)

Again, it seems ambiguous as to whether nodes or
edges are the intended target here, and while the
API can obviously dictate that, it seems a bit like
a case of "in the face of ambiguity, refuse the
temptation to guess" to me.

>  * Graph could also benefit from some bulk update methods,
>   e.g. to update weights on all edges or nodes by passing
>   in a dictionary mapping names to attribute values

Sounds good. Again, the format will need some careful
thought, but you're right that this would improve it
greatly.

>  * Graph could benefit from some bulk query methods,
>   such as .get_node_attributes() and .add_edges().

I'm not sure how the first is different from the existing
.data, what did you have in mind?

>  * Node.__getitem__ could be mapped to .data.__getitem__
>   (following the approach taken by ElementTree); same
>   for Edge.__getitem__

>  * Node.__setitem__ could be mapped to .data.__setitem__
>   (following the approach taken by ElementTree); same
>   for Edge.__setitem__

.data is just a property that returns a dictionary of non
private members right now, so if you're wanting to just say
node.this = 'stuff', you can already do that. Or am I
misreading your intent?

>  * I'd add a DirectedEdge alias for Edge and a new
>   UndirectedEdge as subclass which has is_directed=False;
>   makes code more readable

Sounds good, and thanks for the advice!

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


Re: Where is my namespace?

2009-12-07 Thread Steven D'Aprano
On Mon, 07 Dec 2009 16:25:39 +, Simon Brunning wrote:

> 2009/12/7 vsoler :
[...]
> If you do "from blah import" the imported module itself isn't bound to
> any name in the importing module - you can't get at it at all.

Not quite -- you can get to it if you're willing to do some more work.

>>> from math import sin
>>> mod = __import__(sin.__module__)
>>> mod



Alternatively, you can fetch it from sys.modules directly, but that's 
probably an implementation-specific trick.



>> 3. Mark says: The from statement is really an assignment to names in
>> the importer's scope--a name-copy operation, not a name aliasing.   I
>> don't fully understand what he means. Could anybody explain?

I'm not sure what Mark means by that either. It certainly isn't a copy 
operation, it doesn't duplicate the object you imported. I don't know 
what he means by aliasing, but if he means what I mean by aliasing, then 
I'd say the from statement *is* an aliasing operation: it creates a new 
name that refers to an existing object found by name.


from module import name

is roughly equivalent to:

import module
name = module.name
del module



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


Re: python proxy checker ,change to threaded version

2009-12-07 Thread Terry Reedy

Rhodri James wrote:

On Mon, 07 Dec 2009 18:21:23 -, Terry Reedy  wrote:



 def run(self):
   result = func(*func_args) # matching run_in_thread param names
   callback(result, *callback_args)

Neat, but I think you mean

 if callback is not None:
 callback(result, *callback_args)

for that last line.


yes [blush] I forgot to add 'untested ;-)

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


Re: question about imports in a class

2009-12-07 Thread J
On Mon, Dec 7, 2009 at 16:57, Diez B. Roggisch  wrote:

>> if I put the import at the beginning of the class, it just dawned on
>> me that perhaps I still have to explicitly call the function by class:
>>
>> sysinfo.py
>>
>> class myclass():
>>    import os
>>    def findDMIDecode(self):
>>        for r,d,f in myclass.os.walk(args)
>>
>> is that correct?
>
> It is correct in the sense that it works, but it is by no means good
> practice.

So good practice (I'm doing this for real as well as for my own
education) would be:

sysinfo.py:

import os

class ClassA():
sysinfo.os.walk()

class ClassB():
sysinfo.os.walk()

Sorry for the incredibly entry-level questions...  I'm a more senior
member on a few Linux related lists/groups, and I can certainly
understand if questions as basic as these are annoying, so I'm really
trying to not ask them unless I'm just really confused about
something.

OTOH, if there's something more akin to a python newbies kind of list
where this kind of basic thing is more appropriate, please let me
know, because really, I don't want to clutter up python-list with
stuff that would be better off elsewhere.


-- 

Marie von Ebner-Eschenbach  - "Even a stopped clock is right twice a
day." - 
http://www.brainyquote.com/quotes/authors/m/marie_von_ebnereschenbac.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about imports in a class

2009-12-07 Thread Rhodri James

On Mon, 07 Dec 2009 21:13:25 -, J  wrote:


Just a little newbie confusion about OS imports...

Why does this give me an error:


It's generally helpful to say *what* error you get, including the  
traceback.  Fortunately the source code is enough this time.



class Windows:

def __init__(self):
'''
Constructor
'''
import os

[snip the rest]

This is a bad idea for the reasons you explained yourself.  This import  
statement goes off and loads the module "os" and binds it to the name  
"os".  That name is local to Windows.__init__, just like any other name  
you'd bind here that wasn't explicitly declared to be global.  What you  
actually want is for "os" to be a global name; there are a whole bunch of  
insane ways of doing that, but the usual and least confusing thing to do  
is:


import os

class Windows:
def __init__(self):
# Do stuff...

[the next function was]

   def parseDMI(self):
   # First, find dmidecode.exe
  for root,dirs,files in os.walk('C:\\'):
   for file in files:
   if file == "dmidecode.exe":
self.dmidecodePath=[os.path.normcase(os.path.join(root,file))]
   return "Found DMIDecode.exe at %s" %  
self.dmidecodePath

   break


I assume the formatting is the fault of some mailer somewhere trying to  
wrap the long line.  Such is life.


The break after the return statement is a bit pointless, though.  It's  
never going to get executed.



Also, when I DO have the import statement inside the function
definition, I'm screwing up the join and getting this:
"C:\\dmidecod\\sbin\\dmidecode.exe"

What am I doing that's creating the two \ characters??


At a guess, nothing.  Would I be right in thinking that what you actually  
get is ['C:\\dmidecod\\sbin\\dmidecode.exe'] ?  When you use string  
formatting with a list argument, the list turns itself into a string by  
calling repr() on its elements.  repr() on a string produces something  
that you could cut and paste into code as a string literal; to do that  
without changing the meaning of the string, it escapes the backslashes  
with another backslash.  You can play with this for yourself in the  
interactive shell:


rho...@gnudebst:~$ python
Python 2.6.4 (r264:75706, Nov  2 2009, 14:44:17)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

print r"hello \ world"

hello \ world

s = r"hello \ world"
print s

hello \ world

print str(s)

hello \ world

print repr(s)

'hello \\ world'

a = [r"hello \ world"]
a

['hello \\ world']

print a

['hello \\ world']

print "message = %s" % a

message = ['hello \\ world']





So in my class, I import OS.  Now, in my calling script, do I also
import OS, or will I inheirit OS as part of the class object?


Since you don't bind the name "os" into your class (and you shouldn't,  
since that would definitely be a code smell), you aren't going to inherit  
it.  Just import it where you need to and stop trying to make your life  
difficult!  It doesn't in fact cost you anything, since Python caches the  
module the first time you load it precisely to avoid having to go back to  
the disc every time you want to reload it.


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


Re: question about imports in a class

2009-12-07 Thread Diez B. Roggisch

J schrieb:

On Mon, Dec 7, 2009 at 16:13, J  wrote:


But why does importing in the init not make os available to every
other function in the class?  Do I have to import OS into every
function like this:

class ClassA():

   def func1(self):
   import os

   def func2(self):
   import os


A little more education and playing around and I'm still not quite
sure how to do this...

for the class i'm writing, I want to import os, sys and wmi globally
for the class...
if I put the import at the beginning of the class, it just dawned on
me that perhaps I still have to explicitly call the function by class:

sysinfo.py

class myclass():
import os
def findDMIDecode(self):
for r,d,f in myclass.os.walk(args)

is that correct?


It is correct in the sense that it works, but it is by no means good 
practice.




How about if I have two classes in sysinfo.py and want to import os
globally for BOTH classes to use? Same thing?


import os

class A():
   ...

class B():
   ...


That's it.

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


Re: question about imports in a class

2009-12-07 Thread J
On Mon, Dec 7, 2009 at 16:13, J  wrote:

> But why does importing in the init not make os available to every
> other function in the class?  Do I have to import OS into every
> function like this:
>
> class ClassA():
>
>    def func1(self):
>        import os
>
>    def func2(self):
>        import os

A little more education and playing around and I'm still not quite
sure how to do this...

for the class i'm writing, I want to import os, sys and wmi globally
for the class...
if I put the import at the beginning of the class, it just dawned on
me that perhaps I still have to explicitly call the function by class:

sysinfo.py

class myclass():
import os
def findDMIDecode(self):
for r,d,f in myclass.os.walk(args)

is that correct?

How about if I have two classes in sysinfo.py and want to import os
globally for BOTH classes to use? Same thing?

> Also, when I DO have the import statement inside the function
> definition, I'm screwing up the join and getting this:
> "C:\\dmidecod\\sbin\\dmidecode.exe"

Still not sure about this, but I DO know where I glaringly screwed up.

Originally, the function that finds the dmidecode executable was doing this:

return dmidecodePath # dmidecodePath is, I learned, a list that
contains the results of
 #
self.dmidecodePath=[os.path.normcase(os.path.join(root,file))]

which is wrong, because on the calling side, I was just using the
whole return, which was actually a list, not a string, so this was
fixed simply by:

return dmidecodePath[0]

which now returns simply:

c:\dmidecode\sbin\dmidecode.exe

So that seems correct.

Also, I apologize if my code examples are incomplete.  If they are not
complete "enough" please let me know. I'm doing this for work, and not
sure just how much I can really post without violating any obligations
on my end.. so I'm trying to provide generic examples based on the
code I'm actually trying to write.



-- 

Stephen Leacock  - "I detest life-insurance agents: they always argue
that I shall some day die, which is not so." -
http://www.brainyquote.com/quotes/authors/s/stephen_leacock.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question about imports in a class

2009-12-07 Thread Diez B. Roggisch

J schrieb:

Just a little newbie confusion about OS imports...

Why does this give me an error:

class Windows:

def __init__(self):
'''
Constructor
'''
import os
self.dmidecodePath="" #final path to dmidecode binary

def parseDMI(self):
# First, find dmidecode.exe

for root,dirs,files in os.walk('C:\\'):
for file in files:
if file == "dmidecode.exe":

self.dmidecodePath=[os.path.normcase(os.path.join(root,file))]
return "Found DMIDecode.exe at %s" % self.dmidecodePath
break

Keep in mind that this is very early stage code and does nothing so
far other than telling me where to find dmidecode.exe...

But why does importing in the init not make os available to every
other function in the class?  Do I have to import OS into every
function like this:

class ClassA():

def func1(self):
import os

def func2(self):
import os



No, you don't have to - you just import it once at the module level.




Also, when I DO have the import statement inside the function
definition, I'm screwing up the join and getting this:
"C:\\dmidecod\\sbin\\dmidecode.exe"

What am I doing that's creating the two \ characters??


No, you don't screw it up, "\\" is escape-codes for backslash, and I 
guess what you see is because you work in the interactive interpreter 
that calls repr() on a string when it's displayed - like this:


>>> "\\"
'\\'
>>> print "\\"
\
>>> print repr("\\")
'\\'






Anyway, the first question I'm just asking because I'm still trying to
figure out the heirarchy and how classes work in Python, the other is
because I'm just annoyed...

Also, that brings up a third question:

So in my class, I import OS.  Now, in my calling script, do I also
import OS, or will I inheirit OS as part of the class object?


No, it doesn't, and I'd say you better forget about importing inside 
functions (methods or normal ones) until you have firmer grasp on how 
python namespaces work.




IOW:

script.py

thisclass=myclass():  #where myclass imports os

for r,d,f in os.walk(path)

or would I have to do something like:

for r,d,f in thisclass.os.wall(path)

Or do I just have to also import OS into script.py??


Yes, you do.


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


Re: python proxy checker ,change to threaded version

2009-12-07 Thread Rhodri James

On Mon, 07 Dec 2009 18:21:23 -, Terry Reedy  wrote:


r0g wrote:


The trick to threads is to create a subclass of threading.Thread, define
the 'run' function and call the 'start()' method. I find threading quite
generally useful so I created this simple generic function for running
things in threads...


Great idea. Thanks for posting this.

def run_in_thread( func, func_args=[], callback=None, callback_args=[]  
):


I'm might wary of having mutable defaults for parameters.  They make for  
the most annoying errors.  Even though they're actually safe here, I'd  
still write:


def run_in_thread(func, func_args=(), callback=None, callback_args=()):

out of sheer paranoia.


import threading
class MyThread ( threading.Thread ):
   def run ( self ):
 # Call function
if function_args:
result = function(*function_args)
else:
result = function()


The check is not necessary. by design, f(*[]) == f()
Names do not match param names ;=)


 # Call callback
if callback:
if callback_args:
callback(result, *callback_args)
else:
callback(result)


Ditto. g(x,*[]) == g(x)

 def run(self):
   result = func(*func_args) # matching run_in_thread param names
   callback(result, *callback_args)

Neat, but I think you mean

 if callback is not None:
 callback(result, *callback_args)

for that last line.




MyThread().start()


This is one of the best uses I have seen for a nested class definition.

Ditto!

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


question about imports in a class

2009-12-07 Thread J
Just a little newbie confusion about OS imports...

Why does this give me an error:

class Windows:

def __init__(self):
'''
Constructor
'''
import os
self.dmidecodePath="" #final path to dmidecode binary

def parseDMI(self):
# First, find dmidecode.exe

for root,dirs,files in os.walk('C:\\'):
for file in files:
if file == "dmidecode.exe":

self.dmidecodePath=[os.path.normcase(os.path.join(root,file))]
return "Found DMIDecode.exe at %s" % self.dmidecodePath
break

Keep in mind that this is very early stage code and does nothing so
far other than telling me where to find dmidecode.exe...

But why does importing in the init not make os available to every
other function in the class?  Do I have to import OS into every
function like this:

class ClassA():

def func1(self):
import os

def func2(self):
import os

Also, when I DO have the import statement inside the function
definition, I'm screwing up the join and getting this:
"C:\\dmidecod\\sbin\\dmidecode.exe"

What am I doing that's creating the two \ characters??

Anyway, the first question I'm just asking because I'm still trying to
figure out the heirarchy and how classes work in Python, the other is
because I'm just annoyed...

Also, that brings up a third question:

So in my class, I import OS.  Now, in my calling script, do I also
import OS, or will I inheirit OS as part of the class object?

IOW:

script.py

thisclass=myclass():  #where myclass imports os

for r,d,f in os.walk(path)

or would I have to do something like:

for r,d,f in thisclass.os.wall(path)

Or do I just have to also import OS into script.py??

Thanks!  I know enough to be dangerous, so trying to become less so now...

Jeff
-- 

Ogden Nash  - "The trouble with a kitten is that when it grows up,
it's always a cat." -
http://www.brainyquote.com/quotes/authors/o/ogden_nash.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unittest buffing output on windows?

2009-12-07 Thread Dave Angel

Roy Smith wrote:

I'm running 2.5.1.  I've got a test suite that takes about 15 minutes
to complete.  On my unix boxes, as each test case executes, it prints
out a line (I'm using unittest.TextTestRunner(verbosity=2)) of status,
but on my windows box (running under cygwin), it buffers everything
until the entire test suite is completed.

I can stick sys.stdout.flush() and sys.stderr.flush() in my tearDown()
method, which gets me output, but that doesn't seem like the right
solution.  Is there a better way to get the test runner to flush
output  after every test case?

  
You could try starting Python with the -u switch, which says don't 
buffer stdout. or stderr (at least on Python 2.6).  Try running

   python -?

to see the commandline options.  On the other hand, if it's specifically 
a cygwin problem, I have no idea.


DaveA

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


Re: Two questions ( Oracle and modules )

2009-12-07 Thread Terry Reedy

Gabor Urban wrote:

Hi guys,



2. We have tp write a module that will be imported a couple of places.
Though we must be sure, this module's initialization is performed only
once. How can we implement this? I do not know what to google for.


There are two ways to have one file executed twice to create two module 
objects with two different names. 1) run it as the main file and also 
import it; 2) import with sufficiently different import statements.
(I do not know all the ways to 'accomplish' the latter, but I know it 
can be done.)


So, if you do not execute that module as the main script and you use the 
exact same import statement everywhere, so that the resulting module is 
given the exact same name, then it will be created once and subsequent 
imports will return the same object. This is the normal situation. The 
two cases above are typically accidents.


If you have doubts, put print("Hello, module xxx being initialized.") at 
the top of the module.


tjr



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


Re: dict.fromkeys strange behavior

2009-12-07 Thread Terry Reedy

Tsviki Hirsh wrote:


 >>>dict.fromkeys('at',50)
{'a': 50, 't': 50}

This is obviously not what I wanted.


Not obvious at all. It is precisely what you asked for ;-)

Please read the doc:
5.8. Mapping Types — dict
"classmethod fromkeys(seq[, value])
Create a new dictionary with keys from seq and values set to value."

The purpose of .fromkeys is to create a dict with multiple keys 
initially mapped to the one and same value (None by default). If you do 
not want that, do not use it.


or nearly as clear:
>>> help(dict.fromkeys)
fromkeys(...)
dict.fromkeys(S[,v]) -> New dict with keys from S and values equal 
to v. v defaults to None.


I *strongly* recommend that all beginners peruse the Library Reference 
up through Chapter 5. It you are puzzled by the behavior of a builtin 
object, go back and look up the specific doc.



What are the alternatives?


If you want {'at':50}, say that.

Terry Jan Reedy



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


Graph library for Python (was: Re: python bijection)

2009-12-07 Thread M.-A. Lemburg
geremy condra wrote:
> How interested are you in a C port of graphine? I haven't had
> any specific requests for it, but if its something you need I
> can shuffle it towards the top of the to do pile.

There are two main reasons for a C implementation:

 1. performance

 2. memory footprint

These are important when using graphs with lots of nodes or
when using lots of graphs or operations on graphs in tight
loops.

However, to get such a new type into the core, you need to start
a PEP process and hash out the API some more, esp. with respect
to integration with the other core types, such as dictionaries
and lists of tuples for both creation and as alternative
representation.

Some other comments (in no particular order):

 * the "name" default of using id(node) is not ideal, since
   id(node) will return the address of the object and that
   can be subject to reuse within the lifetime of the process;
   using a global (thread-safe) counter would be safer

 * Graph.__init__ should be able to take a list or set
   of nodes and edges as initializer

 * Graph.__setitem__ could be mapped to .add_node() for
   convenience

 * Graph.__length__ could be mapped to .size() for
   convenience

 * Graph.__iter__ could be mapped to an iterator using
   the fastest traversal method for the graph nodes
   (ie. order does not matter, it's only important that
   all nodes are found as fast as possible)

 * Graph could also benefit from some bulk update methods,
   e.g. to update weights on all edges or nodes by passing
   in a dictionary mapping names to attribute values

 * Graph could benefit from some bulk query methods,
   such as .get_node_attributes() and .add_edges().

 * Node.__getitem__ could be mapped to .data.__getitem__
   (following the approach taken by ElementTree); same
   for Edge.__getitem__

 * Node.__setitem__ could be mapped to .data.__setitem__
   (following the approach taken by ElementTree); same
   for Edge.__setitem__

 * I'd add a DirectedEdge alias for Edge and a new
   UndirectedEdge as subclass which has is_directed=False;
   makes code more readable

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where is my namespace?

2009-12-07 Thread vsoler
On Dec 7, 5:39 pm, Benjamin Kaplan  wrote:
> On Mon, Dec 7, 2009 at 11:10 AM, vsoler  wrote:
> > I take the example from Mark Lutz's excellent book "Learning Python".
>
> > *** In nested1.py  I have:
> > X=99
> > def printer(): print X
>
> > *** In nested2.py  I have:
> > from nested1 import X, printer
> > X=88
> > printer()
>
> > What is amazing is that running nested2.py prints 99 and not 88.
>
> > My questions are:
>
> > 1. Using statement "from" instead of "import" should not create a
> > namespace, at least that's what I think. However, the printer()
> > function is able to find 99 which is residing in...  a namespace?
>
> It doesn't create a namespace. But printer is still in nested1. When
> you do a "from ... import ...", the objects are imported into the
> current namespace. However, they are different names for what is
> currently the same object. They still follow Python's object
> semantics. Here's a simplified overview of what happens
>
> you run nested2.py
>
> 1 namespace: nested2, nothing in it (except for the special stuff)
>
> you run "from nested1 import X, printer". This executes everything in
> nested1 (because def and class are just executable statements in
> Python) and binds X and printer in nested2 to the objects in nested1.
> Note that these are now different names for the same objects.
>
> nested1: X = 99, printer = 
> nested2: X = 99 printer = 
>
> you call X = 88. Because you are rebinding the object, this name
> (nested2.X) is the only one that gets changed. Rebinding doesn't touch
> the object in nested1.
>
> nested1: X=99, printer = 
> nested2: x=88 printer = 
>
> Notice how the name "printer" in nested2 still refers to an object in
> nested1. It is simply another reference, not a new object. When you
> call printer, it's still sitting in nested1 and only sees nested1's X.
>
> > 2. I have tried to access the 88 by qualification from nested2.py.
> > However, I cannot. If using "print nested1.X" in nested2.py I get an
> > error
>
> that's because when you do "from ... import ..." it doesn't import the
> module itself. There's a totally different behavior between from ...
> import and import. Here's what you're trying to do
>
> nested2.py :
>
> import nested1
>
> nested1.X = 88
> nested1.printer()
>
>
>
> > 3. Mark says: The from statement is really an assignment to names in
> > the importer's scope--a name-copy operation, not a name aliasing.   I
> > don't fully understand what he means. Could anybody explain?
>
> Like I showed before, when you change the unqualified "X" in nested2,
> the X in nested1 doesn't change. Using from import is identical to
> doing something like this
>
> a = 1
> b = a
> b = 5
> a == 1 #True
>
> a is a name that temporarily referred to the same object as a.
> However, when you reassign b, it makes the name "b" refer to a
> different object. It doesn't change the object that a and b both refer
> to. When you import using  from ... import, you end up with 2
> different names that, at the start refer to the same object. Mutation
> (if you're using a mutable object) will show up in both namespaces,
> since they are referring to the same object, but assignment does not.
>
> > Thank you very much for your time.
>
> > Vicente Soler
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>

I appreciate the preciseness and clearness of your explanations. Thank
you very much indeed.

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


unittest buffing output on windows?

2009-12-07 Thread Roy Smith
I'm running 2.5.1.  I've got a test suite that takes about 15 minutes
to complete.  On my unix boxes, as each test case executes, it prints
out a line (I'm using unittest.TextTestRunner(verbosity=2)) of status,
but on my windows box (running under cygwin), it buffers everything
until the entire test suite is completed.

I can stick sys.stdout.flush() and sys.stderr.flush() in my tearDown()
method, which gets me output, but that doesn't seem like the right
solution.  Is there a better way to get the test runner to flush
output  after every test case?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python3: Sane way to deal with broken encodings

2009-12-07 Thread Benjamin Kaplan
On Mon, Dec 7, 2009 at 2:16 PM, Johannes Bauer  wrote:
> Bruno Desthuilliers schrieb:
>
>>> Is that possible? If so, how?
>>
>> This might get you started:
>>
>> """
> help(str.decode)
>> decode(...)
>>     S.decode([encoding[,errors]]) -> object
>
> Hmm, this would work nicely if I called "decode" explicitly - but what
> I'm doing is:
>
> #!/usr/bin/python3
> for line in open("broken", "r"):
>        pass
>
> Which still raises the UnicodeDecodeError when I do not even do any
> decoding explicitly. How can I achieve this?
>
> Kind regards,
> Johannes
>

Looking at the python 3 docs, it seems that open takes the encoding
and errors parameters as optional arguments. So you can call
open('broken', 'r',errors='replace')

> --
> "Aus starken Potentialen können starke Erdbeben resultieren; es können
> aber auch kleine entstehen - und "du" wirst es nicht für möglich halten
> (!), doch sieh': Es können dabei auch gar keine Erdbeben resultieren."
> -- "Rüdiger Thomas" alias Thomas Schulz in dsa über seine "Vorhersagen"
> <1a30da36-68a2-4977-9eed-154265b17...@q14g2000vbi.googlegroups.com>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python3: Sane way to deal with broken encodings

2009-12-07 Thread Johannes Bauer
Bruno Desthuilliers schrieb:

>> Is that possible? If so, how?
> 
> This might get you started:
> 
> """
 help(str.decode)
> decode(...)
> S.decode([encoding[,errors]]) -> object

Hmm, this would work nicely if I called "decode" explicitly - but what
I'm doing is:

#!/usr/bin/python3
for line in open("broken", "r"):
pass

Which still raises the UnicodeDecodeError when I do not even do any
decoding explicitly. How can I achieve this?

Kind regards,
Johannes

-- 
"Aus starken Potentialen können starke Erdbeben resultieren; es können
aber auch kleine entstehen - und "du" wirst es nicht für möglich halten
(!), doch sieh': Es können dabei auch gar keine Erdbeben resultieren."
-- "Rüdiger Thomas" alias Thomas Schulz in dsa über seine "Vorhersagen"
<1a30da36-68a2-4977-9eed-154265b17...@q14g2000vbi.googlegroups.com>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generators.

2009-12-07 Thread Jorge Cardona
2009/12/7 Lie Ryan :
> On 12/7/2009 7:22 AM, Jorge Cardona wrote:
>>
>> Hi,
>>
>> I was trying to create a function that receive a generator and return
>> a list but that each elements were computed in a diferent core of my
>> machine. I start using islice function in order to split the job in a
>> way that if there is "n" cores each "i" core will compute the elements
>> i,i+n,i+2n,..., but islice has a weird (to me) behavior, look:
>
> it's nothing weird, python just do what you're telling it to:
>
> transform all x in X with f(x)
>>
>> g = (f(x) for x in X)
>
> then slice the result of that
>>
>> print(list(x for x in islice(g,0,None,2)))
>

When i wrote that first line of code i thought that i was creating a
generator that will later compute  the elements of X with function f,
if i will like to transform them immediately i would use [] instead
(), so, the result is not where i want to slice, even so, is exactly
the same to slice, after or before, the only difference is that after
the compute i'm losing 5 in unnecessary execution of the function f.

> what you want to do is to slice before you transform:
 g = (x for x in islice(X, 0, None, 2))
 print(list(f(x) for x in g))
> eval: 0
> eval: 2
> eval: 4
> eval: 6
> eval: 8
> [0, 2, 4, 6, 8]
>

What i want to do is a function that receive any kind of generator and
execute it in several cores (after a fork) and return the data, so, i
can't slice the set X before create the generator.

>> islice execute the function at the generator and drop the elements
>> that aren't in the slice. I found that pretty weird, the way that i
>> see generators is like an association between and indexing set (an
>> iterator or another generator) and a computation that is made indexed
>> by the indexing set, and islice is like a "transformation" on the
>> indexing set,it doesn't matter the result of the function, the slice
>> should act only on the indexing set, some other "transformation" like
>> takewhile act on the result so, the execution it has to be made, but
>> in the islice, or other "transformation" that act only in the indexing
>> set, the function shouldn't be executed at each element, but only on
>> that new set that result of the application of the "transformation" on
>> the original set.
>
> that seems like an extremely lazy evaluation, I don't know if even a true
> lazy language do that. Python is a strict language, with a few laziness
> provided by generators, in the end it's still a strict language.
>

Yes, it looks like lazy evaluation, but, i don't see why there is not
a better control over the iterable associated to a generator, even
with Python that is a strict language, it will increase the
functionality of it, and the performance too, imagine that you pass a
function that takes 1 sec in run, and for several reason you can't
slice before (as the smp function that i want to create), the final
performance with the actual islice it gets really reduced
Just creating the separation between those transformation that act on
the index(islice, or tee) on those that act on the result(dropwhile,
takewhile, etc.) the control could be fine enough to increase
usability (that's the way i think right now), and you will be able to
combine generator without lose performance.

>> Well, it works for what i need, but is not very neat, and i think that
>> there it should be a formal way to act on the base indexing iterator,
>> such way exists? Is there a better approach to get what i need?
>
> Reverse your operation.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Jorge Eduardo Cardona
jorgeecard...@gmail.com
jorgeecardona.blogspot.com

Linux registered user  #391186
Registered machine#291871

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


Re: python proxy checker ,change to threaded version

2009-12-07 Thread Terry Reedy

r0g wrote:


The trick to threads is to create a subclass of threading.Thread, define
the 'run' function and call the 'start()' method. I find threading quite
generally useful so I created this simple generic function for running
things in threads...


Great idea. Thanks for posting this.


def run_in_thread( func, func_args=[], callback=None, callback_args=[] ):
import threading
class MyThread ( threading.Thread ):
   def run ( self ):

# Call function
if function_args:
result = function(*function_args)
else:
result = function()


The check is not necessary. by design, f(*[]) == f()
Names do not match param names ;=)



# Call callback
if callback:
if callback_args:
callback(result, *callback_args)
else:
callback(result)


Ditto. g(x,*[]) == g(x)

def run(self):
  result = func(*func_args) # matching run_in_thread param names
  callback(result, *callback_args)



MyThread().start()


This is one of the best uses I have seen for a nested class definition.



Suggestions from hardcore pythonistas on how to my make run_in_thread
function more elegant are quite welcome also :)


I shortened it, at least.

Terry Jan Reedy

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


Re: how to convert string function to string method?

2009-12-07 Thread Terry Reedy

Steven D'Aprano wrote:

On Sun, 06 Dec 2009 22:47:48 -0800, Dr. Phillip M. Feldman wrote:


I wrote a handy-dandy function (see below) called "strip_pairs" for
stripping matching pairs of characters from the beginning and end of a
string.  This function works, but I would like to be able to invoke it
as a string method rather than as a function.  Is this possible?


Not exactly. You can subclass string and add such a method:

class MyString(str):
def strip_pairs(self, ...):
...

but then you have to convert every string to a MyString before you use 
it. That way leads to madness.


Better to just use a function. Don't worry, you're allowed to mix 
functional and OO code :)


Unlike certain other languages, Python is not designed around a fetish 
for calling all functions as methods. s.func(arg) is immediately 
translated to cls.func(s,arg) where cls is either the class of s or some 
superclass thereof. Directly writing mod.func(s,arg), where mod is some 
module, is just as good. Methods have three purposes: bundle several 
related functions in a class-specific namespace; inheritance; mapping of 
operations, like '+', to class-specific (special) methods. Modules are 
an alernative namespace bundle.


Terry Jan Reedy

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


Re: IO Gurus: what are the differences between these two methods?

2009-12-07 Thread Joe Riopel
On Mon, Dec 7, 2009 at 12:12 PM, dpapathanasiou
 wrote:
> I have two methods for writing binaries files: the first works with
> data received by a server corresponding to a file upload, and the
> second works with data sent as email attachments.
>
> The odd thing is, they're not interchangeable: if I use the first one
> to saved data parsed from an email attachment, it fails; similarly,
> the second function fails when dealing with an uploaded file data.

When you say it fails, are there any error messages displayed,
exceptions raised, etc? I am not sure what you mean, in regards to
"fails".
-- 
http://mail.python.org/mailman/listinfo/python-list


IO Gurus: what are the differences between these two methods?

2009-12-07 Thread dpapathanasiou
I have two methods for writing binaries files: the first works with
data received by a server corresponding to a file upload, and the
second works with data sent as email attachments.

The odd thing is, they're not interchangeable: if I use the first one
to saved data parsed from an email attachment, it fails; similarly,
the second function fails when dealing with an uploaded file data.

What are the critical differences?

def write_binary_file (folder, filename, f, chunk_size=4096):
"""Write the file data f to the folder and filename combination"""
result = False
if confirm_folder(folder):
try:
file_obj = open(os.path.join(folder, file_base_name
(filename)), 'wb', chunk_size)
for file_chunk in read_buffer(f, chunk_size):
file_obj.write(file_chunk)
file_obj.close()
result = True
except (IOError):
print "file_utils.write_binary_file: could not write '%s'
to '%s'" % (file_base_name(filename), folder)
return result

def write_binary_file (folder, filename, filedata):
"""Write the binary file data to the folder and filename
combination"""
result = False
if confirm_folder(folder):
try:
file_obj = open(os.path.join(folder, file_base_name
(filename)), 'wb')
file_obj.write(filedata)
file_obj.close()
result = True
except (IOError):
print "file_utils.write_binary_file: could not write '%s'
to '%s'" % (file_base_name(filename), folder)
return result

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


Re: python bijection

2009-12-07 Thread geremy condra
On Mon, Dec 7, 2009 at 12:05 PM, M.-A. Lemburg  wrote:
> geremy condra wrote:
>> On Mon, Dec 7, 2009 at 7:51 AM, M.-A. Lemburg  wrote:
>>> Terry Reedy wrote:
 M.-A. Lemburg wrote:

> Integrating an easy-to-use graph library into the collections
> module (and it's C companion) is good idea.
>
>>> This would have to be written in C, though,
>> That's currently in the works, along with database backing.
>> We'd welcome any help though... hint, hint...

 The current thinking among deveopers is that new modules should be
 written and maintained in Python, which all implementations can use,
 with speed-critical parts written in C for speed and imported by the
 Python code.
>>>
>>> I don't think you are speaking for Python developers in general.
>>
>> I believe he's referring to the core developers.
>
> I was as well, being one of them :-)

Misunderstanding: 2, Me: 0... moving on ;)

How interested are you in a C port of graphine? I haven't had
any specific requests for it, but if its something you need I
can shuffle it towards the top of the to do pile.

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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Victor Subervi
On Mon, Dec 7, 2009 at 12:52 PM, Carsten Haese wrote:

> Victor Subervi wrote:
> > Well, if you could point me in the right direction, it would be
> > appreciated. I've tried googling this with no luck. Apparently, "expand"
> > is not a well-documented term in python and, of course, it's an
> > often-used term in English, which further confuses the issue. Yes, I
> > would like to understand this code.
>
> If I remember correctly from one of your earlier posts, "expand" is a
> name of a function that's actually defined in your code. Understanding
> its name won't help you understand what it does. It would do exactly the
> same if it were called "frobnicate" and all instances of "expand" in
> your code were replaced with "frobnicate".
>
> To understand what the function does, look at the function definition
> and pretend that you're the Python interpreter: Follow the code one
> instruction at a time and imagine what each instruction does. If you do
> this correctly, you will understand what the code does. This won't tell
> you *why* it does it, but it's your code, and you put it there to serve
> a particular purpose. If you don't understand its purpose, you shouldn't
> be using it at all.
>

<:-} Right again.
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Victor Subervi
On Mon, Dec 7, 2009 at 12:40 PM, Rami Chowdhury wrote:

> Coming from PHP,
>

Wash your mouth out with soap, Rami! I might not be a good programmer, but
I'm loyal to the Flying Circus. I only did one job, my first one, in PHP
before I became "enlightened" :))


> I can see why you might be confused. Python is not
> PHP -- Python has namespaces and uses them, unlike PHP which IIRC
> shoves nearly everything into the default namespace. So you need to
> start by figuring out where the 'expand' function came from -- I'm
> pretty sure it's not a builtin. Look at the import statements in the
> file to see if the function has been specifically imported from
> somewhere, or if there's a statement of the form "from [module] import
> *". You will then know the module it came from, and be able to either
> look at the code or the documentation for that module, which should
> tell you more.
>

No, it's not imported. These are the only imports:

import sys,os
sys.path.append(os.getcwd())
import MySQLdb
from login import login
import re, string

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


Re: python bijection

2009-12-07 Thread M.-A. Lemburg
geremy condra wrote:
> On Mon, Dec 7, 2009 at 7:51 AM, M.-A. Lemburg  wrote:
>> Terry Reedy wrote:
>>> M.-A. Lemburg wrote:
>>>
 Integrating an easy-to-use graph library into the collections
 module (and it's C companion) is good idea.

>> This would have to be written in C, though,
> That's currently in the works, along with database backing.
> We'd welcome any help though... hint, hint...
>>>
>>> The current thinking among deveopers is that new modules should be
>>> written and maintained in Python, which all implementations can use,
>>> with speed-critical parts written in C for speed and imported by the
>>> Python code.
>>
>> I don't think you are speaking for Python developers in general.
> 
> I believe he's referring to the core developers.

I was as well, being one of them :-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Carsten Haese
Victor Subervi wrote:
> Well, if you could point me in the right direction, it would be
> appreciated. I've tried googling this with no luck. Apparently, "expand"
> is not a well-documented term in python and, of course, it's an
> often-used term in English, which further confuses the issue. Yes, I
> would like to understand this code.

If I remember correctly from one of your earlier posts, "expand" is a
name of a function that's actually defined in your code. Understanding
its name won't help you understand what it does. It would do exactly the
same if it were called "frobnicate" and all instances of "expand" in
your code were replaced with "frobnicate".

To understand what the function does, look at the function definition
and pretend that you're the Python interpreter: Follow the code one
instruction at a time and imagine what each instruction does. If you do
this correctly, you will understand what the code does. This won't tell
you *why* it does it, but it's your code, and you put it there to serve
a particular purpose. If you don't understand its purpose, you shouldn't
be using it at all.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: python bijection

2009-12-07 Thread geremy condra
On Mon, Dec 7, 2009 at 7:51 AM, M.-A. Lemburg  wrote:
> Terry Reedy wrote:
>> M.-A. Lemburg wrote:
>>
>>> Integrating an easy-to-use graph library into the collections
>>> module (and it's C companion) is good idea.
>>>
> This would have to be written in C, though,
 That's currently in the works, along with database backing.
 We'd welcome any help though... hint, hint...
>>
>> The current thinking among deveopers is that new modules should be
>> written and maintained in Python, which all implementations can use,
>> with speed-critical parts written in C for speed and imported by the
>> Python code.
>
> I don't think you are speaking for Python developers in general.

I believe he's referring to the core developers.

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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Rami Chowdhury
On Mon, Dec 7, 2009 at 08:01, Victor Subervi  wrote:
> On Mon, Dec 7, 2009 at 11:29 AM, Carsten Haese 
> wrote:
>>
>> Victor Subervi wrote:
>> > I'll do my best to remember to do that from
>> > now on to:
>> >
>>  allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
>> > 'presCat2': {}}]
>>  level = 0
>>  tree = []
>>  for aTree in allTrees:
>> > ...   for name in sorted(aTree.keys()):
>> > ...     tree.append("%s%s" % ("\t" * level, name))
>> > ...     print aTree([name], level + 1)
>> > ...
>> > Traceback (most recent call last):
>> >   File "", line 4, in ?
>> > TypeError: 'dict' object is not callable
>> 
>> >
>> > So It would seem I need to either figure a way to coerce the dicts into
>> > being tuples a la:
>> > http://code.activestate.com/recipes/361668/
>> > (which looks like a lot of work) or I need to supply tuples instead of
>> > dicts.
>>
>> No. You need to test the actual code you want to test. The code you
>> typed manually has some very significant differences from the code you
>> first posted. For example, one uses <> 1)>>, and the other uses <>. The
>> conclusions you are drawing from this test are meaningless guesses that
>> have nothing to do with solving your actual problem.
>
> Another screw-up. Now that I'm at a computer where I can right click to
> paste the correctly copied code, it executed in the shell just fine. For
> whatever reason, the page itself no longer throws an error, although it's
> still not working properly.
>>
>> > The dicts come from here:
>> >
>> > cursor.execute('select category from categories%s order by Category;' %
>> > (store[0].upper() + store[1:]))
>> > theTree = expand(cursor.fetchall())
>> >
>> > which is the magical code supplied by the lister that does all the heavy
>> > lifting but that I don't understand :-}
>> > Suggestions?
>>
>> Start by understanding the code you're using.
>
> Well, if you could point me in the right direction, it would be appreciated.
> I've tried googling this with no luck. Apparently, "expand" is not a
> well-documented term in python and, of course, it's an often-used term in
> English, which further confuses the issue. Yes, I would like to understand
> this code.

Coming from PHP, I can see why you might be confused. Python is not
PHP -- Python has namespaces and uses them, unlike PHP which IIRC
shoves nearly everything into the default namespace. So you need to
start by figuring out where the 'expand' function came from -- I'm
pretty sure it's not a builtin. Look at the import statements in the
file to see if the function has been specifically imported from
somewhere, or if there's a statement of the form "from [module] import
*". You will then know the module it came from, and be able to either
look at the code or the documentation for that module, which should
tell you more.



Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where is my namespace?

2009-12-07 Thread Bruno Desthuilliers

vsoler a écrit :

I take the example from Mark Lutz's excellent book "Learning Python".

*** In nested1.py  I have:
X=99
def printer(): print X

*** In nested2.py  I have:
from nested1 import X, printer
X=88
printer()

What is amazing is that running nested2.py prints 99 and not 88.


It's "amazing" only if you believe Python's "global" scope is really 
global. Thruth is that in Python, "global" means "module-level" - so 
when you call printer, it resolves X in it's own "global" namespace - 
that is, nested1's global namespace.



My questions are:

1. Using statement "from" instead of "import" should not create a
namespace,


It doesn't.


at least that's what I think. However, the printer()
function is able to find 99 which is residing in...  a namespace?


Yes, cf above. This namespace is created when the nested1.py module is 
first loaded (whether via any form of import or via direct execution of 
nested1 as a script).



2. I have tried to access the 88 by qualification from nested2.py.
However, I cannot. If using "print nested1.X" in nested2.py I get an
error


You have to import the nested1 module itself, ie:

# nested3.py
import nested1

print nested1.X
printer()
nested1.X = 42
printer


3. Mark says: The from statement is really an assignment to names in
the importer's scope--a name-copy operation, not a name aliasing.   I
don't fully understand what he means. Could anybody explain?


Think of Python's namespaces as dicts, where names are keys and 
reference to objects are values.


If you do:

  import nested1

then your current namespace will look like :

  {"nested1" : }

But if you do:

  from nested1 import printer

Then your namespace will look like:

  {"printer" : }


Of course, in this case, the name "printer" in the current namespace is 
bound to the same function object as nested1.printer - but the _name_ 
"printer" is local to your current namespace. If you rebind this name in 
the current namespace, it wont affect nested1's namespace.


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


Re: Where is my namespace?

2009-12-07 Thread Benjamin Kaplan
On Mon, Dec 7, 2009 at 11:10 AM, vsoler  wrote:
> I take the example from Mark Lutz's excellent book "Learning Python".
>
> *** In nested1.py  I have:
> X=99
> def printer(): print X
>
> *** In nested2.py  I have:
> from nested1 import X, printer
> X=88
> printer()
>
> What is amazing is that running nested2.py prints 99 and not 88.
>
> My questions are:
>
> 1. Using statement "from" instead of "import" should not create a
> namespace, at least that's what I think. However, the printer()
> function is able to find 99 which is residing in...  a namespace?
>

It doesn't create a namespace. But printer is still in nested1. When
you do a "from ... import ...", the objects are imported into the
current namespace. However, they are different names for what is
currently the same object. They still follow Python's object
semantics. Here's a simplified overview of what happens

you run nested2.py

1 namespace: nested2, nothing in it (except for the special stuff)

you run "from nested1 import X, printer". This executes everything in
nested1 (because def and class are just executable statements in
Python) and binds X and printer in nested2 to the objects in nested1.
Note that these are now different names for the same objects.

nested1: X = 99, printer = 
nested2: X = 99 printer = 

you call X = 88. Because you are rebinding the object, this name
(nested2.X) is the only one that gets changed. Rebinding doesn't touch
the object in nested1.

nested1: X=99, printer = 
nested2: x=88 printer = 

Notice how the name "printer" in nested2 still refers to an object in
nested1. It is simply another reference, not a new object. When you
call printer, it's still sitting in nested1 and only sees nested1's X.

> 2. I have tried to access the 88 by qualification from nested2.py.
> However, I cannot. If using "print nested1.X" in nested2.py I get an
> error

that's because when you do "from ... import ..." it doesn't import the
module itself. There's a totally different behavior between from ...
import and import. Here's what you're trying to do

nested2.py :

import nested1

nested1.X = 88
nested1.printer()

>
> 3. Mark says: The from statement is really an assignment to names in
> the importer's scope--a name-copy operation, not a name aliasing.   I
> don't fully understand what he means. Could anybody explain?
>

Like I showed before, when you change the unqualified "X" in nested2,
the X in nested1 doesn't change. Using from import is identical to
doing something like this

a = 1
b = a
b = 5
a == 1 #True

a is a name that temporarily referred to the same object as a.
However, when you reassign b, it makes the name "b" refer to a
different object. It doesn't change the object that a and b both refer
to. When you import using  from ... import, you end up with 2
different names that, at the start refer to the same object. Mutation
(if you're using a mutable object) will show up in both namespaces,
since they are referring to the same object, but assignment does not.

> Thank you very much for your time.
>
> Vicente Soler
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where is my namespace?

2009-12-07 Thread Simon Brunning
2009/12/7 vsoler :
> I take the example from Mark Lutz's excellent book "Learning Python".
>
> *** In nested1.py  I have:
> X=99
> def printer(): print X
>
> *** In nested2.py  I have:
> from nested1 import X, printer
> X=88
> printer()
>
> What is amazing is that running nested2.py prints 99 and not 88.
>
> My questions are:
>
> 1. Using statement "from" instead of "import" should not create a
> namespace, at least that's what I think. However, the printer()
> function is able to find 99 which is residing in...  a namespace?

Sorry - you think wrong. All modules have their own namespace.

"from blah import" injects the objects from the imported module
directly into the importing modules namespace, but they are still two
distinct namespaces.

> 2. I have tried to access the 88 by qualification from nested2.py.
> However, I cannot. If using "print nested1.X" in nested2.py I get an
> error

If you do "from blah import" the imported module itself isn't bound to
any name in the importing module - you can't get at it at all.

> 3. Mark says: The from statement is really an assignment to names in
> the importer's scope--a name-copy operation, not a name aliasing.   I
> don't fully understand what he means. Could anybody explain?

Does the above help?

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


Where is my namespace?

2009-12-07 Thread vsoler
I take the example from Mark Lutz's excellent book "Learning Python".

*** In nested1.py  I have:
X=99
def printer(): print X

*** In nested2.py  I have:
from nested1 import X, printer
X=88
printer()

What is amazing is that running nested2.py prints 99 and not 88.

My questions are:

1. Using statement "from" instead of "import" should not create a
namespace, at least that's what I think. However, the printer()
function is able to find 99 which is residing in...  a namespace?

2. I have tried to access the 88 by qualification from nested2.py.
However, I cannot. If using "print nested1.X" in nested2.py I get an
error

3. Mark says: The from statement is really an assignment to names in
the importer's scope--a name-copy operation, not a name aliasing.   I
don't fully understand what he means. Could anybody explain?

Thank you very much for your time.

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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Victor Subervi
On Mon, Dec 7, 2009 at 11:29 AM, Carsten Haese wrote:

> Victor Subervi wrote:
> > I'll do my best to remember to do that from
> > now on to:
> >
>  allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
> > 'presCat2': {}}]
>  level = 0
>  tree = []
>  for aTree in allTrees:
> > ...   for name in sorted(aTree.keys()):
> > ... tree.append("%s%s" % ("\t" * level, name))
> > ... print aTree([name], level + 1)
> > ...
> > Traceback (most recent call last):
> >   File "", line 4, in ?
> > TypeError: 'dict' object is not callable
> 
> >
> > So It would seem I need to either figure a way to coerce the dicts into
> > being tuples a la:
> > http://code.activestate.com/recipes/361668/
> > (which looks like a lot of work) or I need to supply tuples instead of
> > dicts.
>
> No. You need to test the actual code you want to test. The code you
> typed manually has some very significant differences from the code you
> first posted. For example, one uses < 1)>>, and the other uses <>. The
> conclusions you are drawing from this test are meaningless guesses that
> have nothing to do with solving your actual problem.
>

Another screw-up. Now that I'm at a computer where I can right click to
paste the correctly copied code, it executed in the shell just fine. For
whatever reason, the page itself no longer throws an error, although it's
still not working properly.

>
> > The dicts come from here:
> >
> > cursor.execute('select category from categories%s order by Category;' %
> > (store[0].upper() + store[1:]))
> > theTree = expand(cursor.fetchall())
> >
> > which is the magical code supplied by the lister that does all the heavy
> > lifting but that I don't understand :-}
> > Suggestions?
>
> Start by understanding the code you're using.
>

Well, if you could point me in the right direction, it would be appreciated.
I've tried googling this with no luck. Apparently, "expand" is not a
well-documented term in python and, of course, it's an often-used term in
English, which further confuses the issue. Yes, I would like to understand
this code.
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When will Python 3 be fully deployed

2009-12-07 Thread vsoler
On Dec 6, 11:53 pm, "Martin P. Hellwig" 
wrote:
> Edward A. Falk wrote:
>
> 
>
> > For development purposes, you should stick with the oldest version that will
> > actually run your code.  Every time you move to a more modern version, 
> > you're
> > leaving potential users/customers out in the cold.
>
> If the fear of customers disatification prevents you from using a
> certain version of X, you should consider a deployment strategy that
> cuts out dependencies as much as possible. Although this will result in
> a larger end package and possible high amount of duplication, it is
> still preferable to just stop supporting popular platforms or be swamped
> away with bugs due to version mismatches.
>
> --
> MPHhttp://blog.dcuktec.com
> 'If consumed, best digested with added seasoning to own preference.'

Your posts have been very enlightening. Thank you very much!!!
-- 
http://mail.python.org/mailman/listinfo/python-list


opening a connection to quickbooks

2009-12-07 Thread jfabiani
Hi,

Has anyone ever attempted to work with quickbooks in a real time fashion? I
need some advise. I'm trying to work out a way to have real time
updates/inserts/and queries.  I'd also like not to use all the user
licenses. But...

I have discovered that opening a connection to quickbooks takes a long time
(as much as 11 seconds).  Is there a way I can open/create the connection
in a thread (so the rest of the prog can continue) and then use the
connection in the rest of the program to make queries? Or does someone have
a suggestion as how to get around this problem.

I'm using QB's Simple Start Edition - would the enterprise edition have
better performance?

Thanks in advance,

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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Carsten Haese
Victor Subervi wrote:
> I'll do my best to remember to do that from
> now on to:
> 
 allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
> 'presCat2': {}}]
 level = 0
 tree = []
 for aTree in allTrees:
> ...   for name in sorted(aTree.keys()):
> ... tree.append("%s%s" % ("\t" * level, name))
> ... print aTree([name], level + 1)
> ...
> Traceback (most recent call last):
>   File "", line 4, in ?
> TypeError: 'dict' object is not callable

> 
> So It would seem I need to either figure a way to coerce the dicts into
> being tuples a la:
> http://code.activestate.com/recipes/361668/
> (which looks like a lot of work) or I need to supply tuples instead of
> dicts.

No. You need to test the actual code you want to test. The code you
typed manually has some very significant differences from the code you
first posted. For example, one uses <>, and the other uses <>. The
conclusions you are drawing from this test are meaningless guesses that
have nothing to do with solving your actual problem.

> The dicts come from here:
> 
> cursor.execute('select category from categories%s order by Category;' %
> (store[0].upper() + store[1:]))
> theTree = expand(cursor.fetchall())
> 
> which is the magical code supplied by the lister that does all the heavy
> lifting but that I don't understand :-}
> Suggestions?

Start by understanding the code you're using.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Jean-Michel Pichavant

Victor Subervi wrote:



 printTree(aTree[name], level + 1)


... print aTree([name], level + 1)
...
Traceback (most recent call last):
  File "", line 4, in ?
TypeError: 'dict' object is not callable
>>>


Be cautious, you are now executing the same code !
Again, read carefully the error message, you tried to call a dictionary 
: aTree([name], level + 1)


the following code below works fine in a python 2.5 shell:

allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, 
'presCat2': {}}]

level = 0
tree=[]
def printTree(allTrees, level=0):
   for aTree in allTrees:
   for name in sorted(aTree.keys()):
   print '\t' * level, name
   tree.append("%s%s" % ("\t" * level, name))
   printTree(aTree[name], level + 1)


In [9]: run ~/test.py

In [10]: printTree(allTrees)
prodCat1
prodCat2
presCat1
presCat2

In [11]: print tree
['prodCat1', 'prodCat2', 'presCat1', 'presCat2']

Though I'm not sure this is exactly want you want.
Anyway, there is a big probelm happening when  using the following value :
allTrees = [{'prodCat1': {'test':'success'}, 'prodCat2': {}}, 
{'presCat1': {}, 'presCat2': {}}]


printTree take a list in the first place, but when recursively calling 
printTree(aTree[name], level + 1), you pass a dictionary. Your code will 
fail as soon as the dictionary is not empty.


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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Victor Subervi
On Mon, Dec 7, 2009 at 9:36 AM, Jean-Michel Pichavant <
jeanmic...@sequans.com> wrote:

> Victor Subervi wrote:
>
>  On Mon, Dec 7, 2009 at 7:14 AM, Jean-Michel Pichavant <
>> jeanmic...@sequans.com > wrote:
>>
>>Victor Subervi wrote:
>>
>>
>>global printTree = , allTrees =
>>[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
>>'presCat2': {}}]
>> /var/www/html/angrynates.com/cart/catTree.py
>>
>> in
>>printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}},
>>{'presCat1': {}, 'presCat2': {}}], level=0)
>>
>>  12 for name in sorted(aTree.keys()):
>>  13   print '\t' * level, name
>>  14   tree.append("%s%s") % ("\t" * level, name)
>>  15   printTree(aTree[name], level + 1)
>>  16
>>tree = ['%s%s'], tree.append = >object>, level = 0, name = 'prodCat1'
>>
>>TypeError: unsupported operand type(s) for %: 'NoneType' and
>>'tuple'
>> args = ("unsupported operand type(s) for %: 'NoneType'
>>and 'tuple'",)
>>
>>But according to the same error, level = 0 [the NoneType, I
>>presume] and name = 'prodCat1', which is most certainly not a
>>tuple! Why the error?
>>TIA,
>>Victor
>>
>>
>>Come on Victor,
>>
>>Given the error, you can easily see that
>>
>>tree.append("%s%s") % ("\t" * level, name)
>>is involved in the error you get.
>>
>>The correct thing may be
>>tree.append("%s%s" % ("\t" * level, name))
>>
>>It should be easy for you to spot.
>>FYI, tree.append('%s%s') returns None, then ("\t" * level, name)
>>is the tuple applied to None through the % operator. That is why
>>you get the above error.
>>
>>
>> You're absolutely right. This code was supplied to me by a lister. Had I
>> written it myself, I would have caught it. Reading his code, I didn't. I
>> will train myself from now on to write out the code when I come across such
>> errors in the future (and re-write my own as well) so that I can spot such
>> errors:
>>
>> >>> level = 0
>> >>> name = 'one'
>> >>> tree = []
>> >>> tree.append("%s%s" % ("\t" * level, name))
>> >>>
>>
>> However, the code threw one of those blasted HTTP 500 errors and the log
>> had this terse comment:
>>
>> [Mon Dec 07 06:01:23 2009] [error] [client 208.84.198.58] Premature end of
>> script headers: catTree.py
>>
>> Thank you. How helpful. Here's the code:
>>
>> def printTree(allTrees, level=0):
>>  tree = []
>>  for aTree in allTrees:
>>for name in sorted(aTree.keys()):
>>  tree.append("%s%s" % ("\t" * level, name))
>>  printTree(aTree[name], level + 1)
>>
>> The problem must be occurring in the last line which creates a loop since
>> printTree is called only once at the very end of the entire script (where it
>> is returned to the referrer), but it seems perfectly logical to me. Please
>> advise.
>> V
>>
> Is allTrees a dict or a list ?
> Did you try to define what could be a allTree value and test your code step
> by step in a python shell ?
> That's what is missing in your example: an allTrees values for us to
> execute your code and check what is going wrong.
>

That was a great suggestion. I'll do my best to remember to do that from now
on to:

>>> allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
'presCat2': {}}]
>>> level = 0
>>> tree = []
>>> for aTree in allTrees:
...   for name in sorted(aTree.keys()):
... tree.append("%s%s" % ("\t" * level, name))
... print aTree([name], level + 1)
...
Traceback (most recent call last):
  File "", line 4, in ?
TypeError: 'dict' object is not callable
>>>

So It would seem I need to either figure a way to coerce the dicts into
being tuples a la:
http://code.activestate.com/recipes/361668/
(which looks like a lot of work) or I need to supply tuples instead of
dicts. The dicts come from here:

cursor.execute('select category from categories%s order by Category;' %
(store[0].upper() + store[1:]))
theTree = expand(cursor.fetchall())

which is the magical code supplied by the lister that does all the heavy
lifting but that I don't understand :-}
Suggestions?
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: postgresql_autodoc in Python?

2009-12-07 Thread Tino Wildenhain

Am 07.12.2009 15:14, schrieb Jean-Michel Pichavant:

Wolfgang Keller wrote:

Hello,

has anyone ever implemented something similar to postgresql_autodoc in
Python?

TIA,

Sincerely,

Wolfgang


If by postgresql_autodoc you mean tools for generating html
documentation from python code, yes.

Starting from the ugliest:

- pydoc
- epydoc
- sphinx

are 3 of them. I would suggest epydoc, the best look&feel/complexity
ratio. Sphinx is used to generate any kind of documentation (look at
http://docs.python.org/ it's build with Sphinx) but I never figured out
how to easily build documentation from code despite it states it is
posible. With epydoc you only need to press the GO button.


I suspect he meant documenting postgres database structure.

One way would be reading the informaton_schema* and generating python
code out of it then use any of the above methods to finally document the
stuff :-)

*) http://www.alberton.info/postgresql_meta_info.html

Regards
Tino



smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Jean-Michel Pichavant

Victor Subervi wrote:
On Mon, Dec 7, 2009 at 7:14 AM, Jean-Michel Pichavant 
mailto:jeanmic...@sequans.com>> wrote:


Victor Subervi wrote:


global printTree = , allTrees =
[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
'presCat2': {}}]
 /var/www/html/angrynates.com/cart/catTree.py

 in
printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}},
{'presCat1': {}, 'presCat2': {}}], level=0)

  12 for name in sorted(aTree.keys()):
  13   print '\t' * level, name
  14   tree.append("%s%s") % ("\t" * level, name)
  15   printTree(aTree[name], level + 1)
  16
tree = ['%s%s'], tree.append = , level = 0, name = 'prodCat1'

TypeError: unsupported operand type(s) for %: 'NoneType' and
'tuple'
 args = ("unsupported operand type(s) for %: 'NoneType'
and 'tuple'",)

But according to the same error, level = 0 [the NoneType, I
presume] and name = 'prodCat1', which is most certainly not a
tuple! Why the error?
TIA,
Victor


Come on Victor,

Given the error, you can easily see that

tree.append("%s%s") % ("\t" * level, name)
is involved in the error you get.

The correct thing may be
tree.append("%s%s" % ("\t" * level, name))

It should be easy for you to spot.
FYI, tree.append('%s%s') returns None, then ("\t" * level, name)
is the tuple applied to None through the % operator. That is why
you get the above error.


You're absolutely right. This code was supplied to me by a lister. Had 
I written it myself, I would have caught it. Reading his code, I 
didn't. I will train myself from now on to write out the code when I 
come across such errors in the future (and re-write my own as well) so 
that I can spot such errors:


>>> level = 0
>>> name = 'one'
>>> tree = []
>>> tree.append("%s%s" % ("\t" * level, name))
>>>

However, the code threw one of those blasted HTTP 500 errors and the 
log had this terse comment:


[Mon Dec 07 06:01:23 2009] [error] [client 208.84.198.58] Premature 
end of script headers: catTree.py


Thank you. How helpful. Here's the code:

def printTree(allTrees, level=0):
  tree = []
  for aTree in allTrees:
for name in sorted(aTree.keys()):
  tree.append("%s%s" % ("\t" * level, name))
  printTree(aTree[name], level + 1)

The problem must be occurring in the last line which creates a loop 
since printTree is called only once at the very end of the entire 
script (where it is returned to the referrer), but it seems perfectly 
logical to me. Please advise.

V

Is allTrees a dict or a list ?
Did you try to define what could be a allTree value and test your code 
step by step in a python shell ?
That's what is missing in your example: an allTrees values for us to 
execute your code and check what is going wrong.


JM

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


Re: postgresql_autodoc in Python?

2009-12-07 Thread Jean-Michel Pichavant

Wolfgang Keller wrote:

Hello,

has anyone ever implemented something similar to postgresql_autodoc in Python?

TIA,

Sincerely,

Wolfgang

  
If by postgresql_autodoc you mean tools for generating html 
documentation from python code, yes.


Starting from the ugliest:

- pydoc
- epydoc
- sphinx

are 3 of them. I would suggest epydoc, the best look&feel/complexity 
ratio. Sphinx is used to generate any kind of documentation (look at 
http://docs.python.org/ it's build with Sphinx) but I never figured out 
how to easily build documentation from code despite it states it is 
posible. With epydoc you only need to press the GO button.


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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Victor Subervi
On Mon, Dec 7, 2009 at 7:14 AM, Jean-Michel Pichavant <
jeanmic...@sequans.com> wrote:

> Victor Subervi wrote:
>
>>
>> global printTree = , allTrees = [{'prodCat1': {},
>> 'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}]
>>  /var/www/html/angrynates.com/cart/catTree.py <
>> http://angrynates.com/cart/catTree.py> in
>> printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
>> 'presCat2': {}}], level=0)
>>
>>   12 for name in sorted(aTree.keys()):
>>   13   print '\t' * level, name
>>   14   tree.append("%s%s") % ("\t" * level, name)
>>   15   printTree(aTree[name], level + 1)
>>   16
>> tree = ['%s%s'], tree.append = ,
>> level = 0, name = 'prodCat1'
>>
>> TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
>>  args = ("unsupported operand type(s) for %: 'NoneType' and 'tuple'",)
>>
>> But according to the same error, level = 0 [the NoneType, I presume] and
>> name = 'prodCat1', which is most certainly not a tuple! Why the error?
>> TIA,
>> Victor
>>
>
> Come on Victor,
>
> Given the error, you can easily see that
>
> tree.append("%s%s") % ("\t" * level, name)
> is involved in the error you get.
>
> The correct thing may be
> tree.append("%s%s" % ("\t" * level, name))
>
> It should be easy for you to spot.
> FYI, tree.append('%s%s') returns None, then ("\t" * level, name) is the
> tuple applied to None through the % operator. That is why you get the above
> error.
>

You're absolutely right. This code was supplied to me by a lister. Had I
written it myself, I would have caught it. Reading his code, I didn't. I
will train myself from now on to write out the code when I come across such
errors in the future (and re-write my own as well) so that I can spot such
errors:

>>> level = 0
>>> name = 'one'
>>> tree = []
>>> tree.append("%s%s" % ("\t" * level, name))
>>>

However, the code threw one of those blasted HTTP 500 errors and the log had
this terse comment:

[Mon Dec 07 06:01:23 2009] [error] [client 208.84.198.58] Premature end of
script headers: catTree.py

Thank you. How helpful. Here's the code:

def printTree(allTrees, level=0):
  tree = []
  for aTree in allTrees:
for name in sorted(aTree.keys()):
  tree.append("%s%s" % ("\t" * level, name))
  printTree(aTree[name], level + 1)

The problem must be occurring in the last line which creates a loop since
printTree is called only once at the very end of the entire script (where it
is returned to the referrer), but it seems perfectly logical to me. Please
advise.
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: postgresql_autodoc in Python?

2009-12-07 Thread Simon Brunning
2009/12/6 Wolfgang Keller :
> Hello,
>
> has anyone ever implemented something similar to postgresql_autodoc in Python?

Dunno - what is it?

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


Re: Float precision and float equality

2009-12-07 Thread Mark Dickinson
On Dec 7, 12:16 am, David Cournapeau  wrote:
> If you can depend on IEEE 754 semantics, one relatively robust method
> is to use the number of representable floats between two numbers. The
> main advantage compared to the proposed methods is that it somewhat
> automatically takes into account the amplitude of input numbers:

FWIW, there's a function that can be used for this in Lib/test/
test_math.py in Python svn;  it's used to check that math.gamma isn't
out by more than 20 ulps (for a selection of test values).

def to_ulps(x):
"""Convert a non-NaN float x to an integer, in such a way that
adjacent floats are converted to adjacent integers.  Then
abs(ulps(x) - ulps(y)) gives the difference in ulps between two
floats.

The results from this function will only make sense on platforms
where C doubles are represented in IEEE 754 binary64 format.

"""
n = struct.unpack('http://mail.python.org/mailman/listinfo/python-list


Re: What is the significance of after() in this code?

2009-12-07 Thread Neil Cerutti
On 2009-12-07, Neil Cerutti  wrote:
> On 2009-12-07, W. eWatson  wrote:
>> See Subject.
>>  def StackImages(self):
>>  self.Upload("P")
>>  self.after_id = self.master.after(1,self.GetFrameOne)
>
> It's a violation of the Law of Demeter.

Actually, I take that back. Dur.

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


Re: Float precision and float equality

2009-12-07 Thread Mark Dickinson
On Dec 6, 7:34 pm, Anton81  wrote:
> I do some linear algebra and whenever the prefactor of a vector turns
> out to be zero, I want to remove it.

Hmm.  Comparing against zero is something of a special case.  So you'd
almost certainly be doing an 'if abs(x) < tol: ...' check, but the
question is what value to use for tol, and that (again) depends on
what you're doing.  Perhaps 'tol' could be something like 'eps *
scale', where 'eps' is an indication of the size of relative error
you're prepared to admit (eps = 1e-12 might be reasonable;  to allow
for rounding errors, it should be something comfortably larger than
the machine epsilon sys.float_info.epsilon, which is likely to be
around 2e-16 for a typical machine), and 'scale' is something closely
related to the scale of your problem: in your example, perhaps scale
could be the largest of all the prefactors you have, or some sort of
average of all the prefactors.  There's really no one-size-fits-all
easy answer here.

> I'd like to keep the system comfortable. So basically I should write a
> new class for numbers that has it's own __eq__ operator?

That's probably not a good idea, for the reasons that Carl Banks
already enumerated.

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


Re: What is the significance of after() in this code?

2009-12-07 Thread Neil Cerutti
On 2009-12-07, W. eWatson  wrote:
> See Subject.
>  def StackImages(self):
>  self.Upload("P")
>  self.after_id = self.master.after(1,self.GetFrameOne)

It's a violation of the Law of Demeter.

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


Re: python bijection

2009-12-07 Thread M.-A. Lemburg
Carl Banks wrote:
> On Dec 4, 4:42 pm, Lie Ryan  wrote:
>> On 12/5/2009 9:41 AM, Carl Banks wrote:
>>
>>
>>
>>
>>
>>> On Dec 4, 12:46 pm, geremy condra  wrote:
>>> more common than full-blown graph package).
 Sure, its a tree, which is also a graph. In this case it looks to
 me more like a directed acyclic graph than anything, but its
 pretty much just semantics since the interface is functionally
 equivalent.
>>
>>> I'd have to agree with Lie, yes a tree is a graph, but it's simply not
>>> an argument that Python community is grasping for graph structures.
>>> It's like arguing that the Python community could benefit from a
>>> quaternion type, because quaternions are actually heavily used in
>>> Python, because a scalar number is a quarternion.
>>
>>> Carl Banks
>>
>>> (Would be +1 on a good graph implementation... just not because of
>>> ElementTree.)
>>
>> I think this could be an interpretation of the Zen:
>>
>> Simple is better than complex.
>> Complex is better than complicated.
>>
>> can be read as:
>> List is better than Tree
>> Tree is better than Graph
>>
>> not having Tree and Graph package in the standard library force most
>> people to find List-based solution. And people that know they need
>> graphs will find them in 3rd party modules. I have needed Trees a few
>> times in python, but very rarely a Graph (except for playing around).
>> YMDWV (your mileage definitely will vary).

Trees are only a subset of general graphs and graphs often provide
a more intuitive approach to problem representation than trying
to squash all information into a tree or list.

For certain problems, like e.g. dependency checking or routing,
you can easily run into cycles which cannot be represented by
trees (*).

Furthermore, the property of being cycle-free (acyclic)
is often one of the things you want to find out when dealing
with graph data sets.

(*) Note that Python does allow creating lists with cyclic references,
but such usage is not really encouraged and will lead to delays in
garbage collection:

>>> l = [1,2,3]
>>> l[2] = l
>>> l
[1, 2, [...]]

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two questions ( Oracle and modules )

2009-12-07 Thread Michael Crute
On Mon, Dec 7, 2009 at 6:57 AM, Gabor Urban  wrote:
> 1. I have choice to introduce Python to my new employee. We are to
> write and application that uses databases stored partially in Oracle
> 10 and partially in Oracle 11. We have to execute standard SQL
> commands and stored procedures as well. Which is best conection /
> module to use. A couple of years ago we had DCOracle2 for the same
> task, but is written for Oracle8, so I gues it might not be a good
> idea to use. What is your oppinion?

cx_Oracle is the best way to connect to Oracle databases, DCOracle2
hasn't been maintained in some time. http://cx-oracle.sourceforge.net/

-mike

-- 
Michael E. Crute
http://mike.crute.org

It is a mistake to think you can solve any major problem just with
potatoes. --Douglas Adams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-12-07 Thread M.-A. Lemburg
Terry Reedy wrote:
> M.-A. Lemburg wrote:
> 
>> Integrating an easy-to-use graph library into the collections
>> module (and it's C companion) is good idea.
>>
 This would have to be written in C, though,
>>> That's currently in the works, along with database backing.
>>> We'd welcome any help though... hint, hint...
> 
> The current thinking among deveopers is that new modules should be
> written and maintained in Python, which all implementations can use,
> with speed-critical parts written in C for speed and imported by the
> Python code.

I don't think you are speaking for Python developers in general.

The usual approach is to develop a prototype in Python and then
rewrite it in C. Since the prototype is already there, what
remains is the rewrite to get better performance.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are routine objects guaranteed mutable & with dictionary?

2009-12-07 Thread Duncan Booth
"Alf P. Steinbach"  wrote:

> The question is what guarantees or absence thereof the language
> specification, PEPs, intentions, whatever gives/has.

See the documentation: http://docs.python.org/3.1/reference/datamodel.html
> 
> 
>> BTW, it's a function, not a "routine"
> 
> Wikipedia is your friend,  http://en.wikipedia.org/wiki/Subroutine>. 
> 
Yes, but if you use the same terminology as everyone else you'll look under  
'User-defined functions' on the page I referenced above. Looking under 
'routine' or 'subroutine' won't get you very far.



-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't XP Pro Show Size, Time and Date Mod of a Created File?

2009-12-07 Thread W. eWatson

Mensanator wrote:

On Dec 5, 11:38�pm, "W. eWatson"  wrote:

I created a folder, and wrote a file to it. When I look at what files
are in it, they are correct. However, The Size, Type, and Date Mod are
not shown. Why am I missing those columns? I'm writing files with a
suffix of dat, which seem only to match up with video CD movie.


Got to the View menu and select Details.

Once in the Details view, click on the column
headings and select which details to view.


That's strange. I had gone to details before without any luck, but now 
it's showing the above columns.

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


Why Doesn't XP Pro Show Size, Time and Date Mod of a Created File?

2009-12-07 Thread W. eWatson
I created a folder, and wrote a file to it. When I look at what files 
are in it, they are correct. However, The Size, Type, and Date Mod are 
not shown. Why am I missing those columns? I'm writing files with a 
suffix of dat, which seem only to match up with video CD movie.

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


Re: Why Can't I Delete a File I Created with Win XP?

2009-12-07 Thread John Machin
On Dec 5, 9:57 pm, "W. eWatson"  wrote:
[snip]
>          s = self.current_path
s referred to something ...
>          s = "Analysis"
but now s refers to "Analysis" ... at best, there is redundant &
confusing code; at worst, the source of your problem.

>          s = os.path.join("Analysis",s)
and now s refers to r"Analysis\Analysis" (on your platform)
>          print "s joined ",s    <- debug print

[snip]

> There is no file created, just the folders Analysis\Analysis. One too
> many. The second Analysis shows as an icon for a file of size 0KB.
>
> I printed with the debug print above:
>    Path for Histogram Events\v20070206_055012.06.dat
>    s joined  
> Analysis\Analysis should only be Analysis.

Huh?? s = os.path.join("fubar", "fubar") should produce r"fubar
\fubar" (as documented) ... If you don't want s to refer to r"Analysis
\Analysis", then quite simply don't do that!

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


Re: Organization of GUIs

2009-12-07 Thread Michael
On Dec 5, 11:50 pm, zeph  wrote:
> I highly recommend reading the Cocoa documentation, which has volumes
> on all sorts of things like this.  Here's a link that talks about
> views in that context, and should give you more ideas about well-
> designed GUI layouts:http://bit.ly/6b8PYh

Cool link. Thanks!
-Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to timeout when waiting for raw_input from user ?

2009-12-07 Thread Rune Strand
On Dec 5, 3:42 pm, Maxim Khitrov  wrote:

> I'm not talking about the Timer, I'm talking about the original
> question. There's nothing (that I know of) you can do with a Timer on
> Windows to interrupt a raw_input call.

That is true. But if the issue here is to present a question, and
await answer for N seconds, before pusing next question, Timer() can
be used to call SendKeys() and have it send "{ENTER}" to raw_input.

http://www.rutherfurd.net/python/sendkeys/index.html

SendKeys is Win-only
-- 
http://mail.python.org/mailman/listinfo/python-list


postgresql_autodoc in Python?

2009-12-07 Thread Wolfgang Keller
Hello,

has anyone ever implemented something similar to postgresql_autodoc in Python?

TIA,

Sincerely,

Wolfgang

-- 
NO "Courtesy Copies" PLEASE!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of ** (unexpected/inconsistent?)

2009-12-07 Thread Albert van der Horst
In article <87eingrbh9@benfinney.id.au>,
Ben Finney   wrote:
>Lie Ryan  writes:
>
>> I generally do not expect operator precedence to be reliable at all
>
>Have another read of the thread. The OP's confusion was not over
>operator precedence, but over how names resolve to values in
>expressions.

Operator precedence comes naturally into this matter.
For example, in algol 68 -3**2 is parsed as
  (-3)**2
because of the simple rule that all unary operators have precedence
over all binary operators.

(It is a good rule, and this is about the only way to get a
somewhat surprising result. Unary operators -- as long as they
are always put up front -- need not have
a precedence among themselves, so with this rule they don't
need a precedence full stop. )

>Ben Finney

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Why Doesn't XP Pro Show Size, Time and Date Mod of a Created File?

2009-12-07 Thread Mensanator
On Dec 5, 11:38�pm, "W. eWatson"  wrote:
> I created a folder, and wrote a file to it. When I look at what files
> are in it, they are correct. However, The Size, Type, and Date Mod are
> not shown. Why am I missing those columns? I'm writing files with a
> suffix of dat, which seem only to match up with video CD movie.

Got to the View menu and select Details.

Once in the Details view, click on the column
headings and select which details to view.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hola

2009-12-07 Thread Chris Rebert
2009/12/6 franki fuentes cueto :
> hola soy un pequeño programador y quiesiera pedirles ayuda para programar en
> python, no se si me podrian mandar ejemplos para poder empezar, y como
> terminarlo para que se ejecute, me entiendes , aver sime ayudan gracias

Esta lista de discusión es en Inglés.

Hay una lista de discusión en Español aqui:
http://listas.aditel.org/listinfo/python-es

Salud,
Chris
--
Sé solamente un poquito de Español.
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: High-performance Python websites

2009-12-07 Thread Dr. Marco
On Wed, 25 Nov 2009 21:21:25 -0800 (PST), 
Nick Mellor  wrote :
Hi,

> I wasn't aware that Google used Python for running their Google groups
> servers. Can you confirm that? The only place
> I've seen Google explicitly use Python on their web front end is in
> the Google Ads tests.
> 
> I am impressed by the responsiveness of lawrence.com, ljworld.com and
> others on the Django home page (http://www.djangoproject.com/)

I'm running two sites using Django which are certainly infinitely more
modest and much less visited than the ones you quote but which
nevertheless are extremely responsive compared to other frameworks
tested, using a single machine (http://nonlineaire.univ-lille1.fr/SNL/
and http://nonlineaire.univ-lille1.fr/GDR3070/).

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


Re: What is the significance of after() in this code?

2009-12-07 Thread W. eWatson

Martin P. Hellwig wrote:

W. eWatson wrote:

See Subject.
def StackImages(self):
self.Upload("P")
self.after_id = self.master.after(1,self.GetFrameOne)


If you are talking tkinter here, it is an alarm callback.
See http://effbot.org/tkinterbook/widget.htm

Very good. I recall encountering this once before. I knew it had to be 
something like that, since the code does not provide a method called 
after().

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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Jean-Michel Pichavant

Victor Subervi wrote:


global printTree = , allTrees = [{'prodCat1': {}, 
'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}]
 /var/www/html/angrynates.com/cart/catTree.py 
 in 
printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, 
'presCat2': {}}], level=0)

   12 for name in sorted(aTree.keys()):
   13   print '\t' * level, name
   14   tree.append("%s%s") % ("\t" * level, name)
   15   printTree(aTree[name], level + 1)
   16
tree = ['%s%s'], tree.append = object>, level = 0, name = 'prodCat1'


TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
  args = ("unsupported operand type(s) for %: 'NoneType' and 
'tuple'",)


But according to the same error, level = 0 [the NoneType, I presume] 
and name = 'prodCat1', which is most certainly not a tuple! Why the error?

TIA,
Victor


Come on Victor,

Given the error, you can easily see that
tree.append("%s%s") % ("\t" * level, name)
is involved in the error you get.

The correct thing may be
tree.append("%s%s" % ("\t" * level, name))

It should be easy for you to spot.
FYI, tree.append('%s%s') returns None, then ("\t" * level, name) is the 
tuple applied to None through the % operator. That is why you get the 
above error.


Cheers,

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


Two questions ( Oracle and modules )

2009-12-07 Thread Gabor Urban
Hi guys,

I have two questions.

1. I have choice to introduce Python to my new employee. We are to
write and application that uses databases stored partially in Oracle
10 and partially in Oracle 11. We have to execute standard SQL
commands and stored procedures as well. Which is best conection /
module to use. A couple of years ago we had DCOracle2 for the same
task, but is written for Oracle8, so I gues it might not be a good
idea to use. What is your oppinion?

2. We have tp write a module that will be imported a couple of places.
Though we must be sure, this module's initialization is performed only
once. How can we implement this? I do not know what to google for.

Gabor

-- 
Linux: Choice of a GNU Generation
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to convert string function to string method?

2009-12-07 Thread Bruno Desthuilliers

r0g a écrit :
(snip)
 > I've never tried it but I think it is possible to inject new methods

into existing classes, see...


Doesn't work for most builtin types - for both performances and sanity 
reasons.


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


Re: unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Xavier Ho
Hi Victor,

the .append function doesn't return anything, so it's a None. And you should
have it inside the parentheses.

>>> tree.append("%s%s" % ("\t" * level, name))

is probably what you're after.

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


unsupported operand type(s) for %: 'NoneType' and 'tuple'

2009-12-07 Thread Victor Subervi
Hi;
I get the following error:

/var/www/html/angrynates.com/cart/createCats2.py
  111 
  112 
  113 '''
  114
  115 createCats2()
createCats2 = 
 /var/www/html/angrynates.com/cart/createCats2.py in createCats2()
   85   for standAloneStore in storePrimaryStandAlone:
   86 allStores.append(standAloneStore)
   87   tree = catTree(allStores)
   88   for store in allStores:
   89 i = 0
tree undefined, global catTree = , allStores =
['products', 'prescriptions']
 /var/www/html/angrynates.com/cart/catTree.py in
catTree(allStores=['products', 'prescriptions'])
   76   returnFlag = 'gotNoStuff'
   77   if returnFlag == 'gotStuff':
   78 return printTree(allTrees)
   79   else:
   80 return ''
global printTree = , allTrees = [{'prodCat1': {},
'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}]
 /var/www/html/angrynates.com/cart/catTree.py in
printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
'presCat2': {}}], level=0)
   12 for name in sorted(aTree.keys()):
   13   print '\t' * level, name
   14   tree.append("%s%s") % ("\t" * level, name)
   15   printTree(aTree[name], level + 1)
   16
tree = ['%s%s'], tree.append = ,
level = 0, name = 'prodCat1'

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
  args = ("unsupported operand type(s) for %: 'NoneType' and 'tuple'",)

But according to the same error, level = 0 [the NoneType, I presume] and
name = 'prodCat1', which is most certainly not a tuple! Why the error?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >