locating data_files

2011-03-14 Thread Darren Hart
I'm using dist_utils to install some data along with my module, but I
haven't been able to sort out where that data gets installed from
within the module. The user can specify --prefix=/usr/local for
instance, how can I know where to look for the data files?

My setup.py installs a glade file and several png images for example:

data_files=[('share/braindump/images', ["images/countdown-00.png",
  "images/countdown-01.png",
  "images/countdown-02.png",
  "images/countdown-03.png",
  "images/countdown-04.png",
  "images/countdown-05.png",
  "images/countdown-06.png",
  "images/countdown-07.png",
  "images/countdown-08.png",
  "images/countdown-09.png",
  "images/countdown-10.png",
  "images/countdown-11.png",]),
  ('share/braindump/glade', ['glade/braindump.glade']),
  ('share/braindump/glade', ['glade/braindump.png']),
  ('share/applications', ['data/braindump.desktop']),
  ('share/icons/hicolor/scalable/apps',
['data/icons/braindump.svg']),
  ('share/icons/hicolor/16x16/apps',
['data/icons/16x16/braindump.png']),
  ('share/icons/hicolor/22x22/apps',
['data/icons/22x22/braindump.png']),
  ('share/icons/hicolor/24x24/apps',
['data/icons/24x24/braindump.png']),
  ('share/icons/hicolor/32x32/apps',
['data/icons/32x32/braindump.png']),
  ('share/icons/hicolor/36x36/apps',
['data/icons/36x36/braindump.png']),
  ('share/icons/hicolor/48x48/apps',
['data/icons/48x48/braindump.png']),
  ('share/icons/hicolor/64x64/apps',
['data/icons/64x64/braindump.png']),
  ('share/icons/hicolor/72x72/apps',
['data/icons/72x72/braindump.png']),
  ('share/icons/hicolor/96x96/apps',
['data/icons/96x96/braindump.png']),
  ('share/icons/hicolor/128x128/apps',
['data/icons/128x128/braindump.png']),
  ('share/icons/hicolor/192x192/apps',
['data/icons/192x192/braindump.png']),
  ('share/icons/hicolor/256x256/apps',
['data/icons/256x256/braindump.png'])],

How can I determine the full path to those images from within the module?

GUI(os.path.join(WHAT_IS_THE_PREFIX, "share/braindump/glade/braindump.glade"))

Thanks,

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


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Stefan Behnel

Steven D'Aprano, 13.03.2011 13:59:

The removal of cmp from the sort method of lists is probably the most
disliked change in Python 3. On the python-dev mailing list at the
moment, Guido is considering whether or not it was a mistake.

If anyone has any use-cases for sorting with a comparison function that
either can't be written using a key function, or that perform really
badly when done so, this would be a good time to speak up.


As Raymond Hettinger and Daniel Stutzbach pointed out in that thread, the 
memory overhead is much lower in Python 3.2 and also depends on the usage. 
If memory is a problem, it can still be traded for time by sorting in 
multiple (stable) passes with smaller keys. It rarely is a problem in 
practice, though, and Guido's initial post seems to suggest that as well.


Stefan

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


logging and PyQt4

2011-03-14 Thread Adrian Casey
I have a multi-threaded PyQt4 application which has both a GUI and command-
line interface.  I am using Qt4's threading because from what I have read, 
it is more efficient than the native python threading module.  Also, given 
most users will probably use the GUI, it seemed to make sense.  

I want a flexible, threadsafe logging facility for my application so I was 
thinking of using python's logging module.  I need a logger that can log to 
the GUI or a terminal depending on how the application is invoked.

So, my question is -:

Is it wise to use python's logging module in conjunction with Qt4 threads?  
If not, what are my options apart from writing my own logging module?

If it is OK, then I would like to know how to subclass the logging class so 
that instead of sending output to stdout (as in StreamHandler), it emits Qt4 
signals instead.

Any help would be appreciated.

Thank you.
Adrian Casey.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Just finished reading of "What’s New In Python 3.0"

2011-03-14 Thread D'Arcy J.M. Cain
On Fri, 11 Mar 2011 06:30:35 -0800
Westley Martínez  wrote:
[repeated posing elided]

> n00m: GET A BLOG.

Is it so hard to simply add him to your killfile and move on?  Those of
us who have already done so get to see his postings anyway if people
are going to reply and repeat his trolls.

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


Re: Don't Want Visitor To See Nuttin'

2011-03-14 Thread Ian Kelly
On Fri, Mar 11, 2011 at 10:40 AM, Victor Subervi
 wrote:
> Um...just for the record, these guys have ben featured on the FRONT PAGES
> OF:
> [SNIPPED]

I don't care if the company was founded by the second coming of Jesus
Christ; I just call it like I see it.

> They're ligit :)

Oh, I have no doubt that the tour is real.  I'm sure that it will be
conducted exactly as they describe it, and due to confirmation bias
and the highly subjective nature of "global transformation", I'm even
willing to bet that the participants will return home satisfied and
convinced that they have taken part in some cosmically important
historical event.  The easiest people to take advantage of are the
ones who don't even realize they've been swindled.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDLE doesn't start

2011-03-14 Thread Ceonn Bobst
Hi Gennadiy,
 
Here is the output I get:
C:\Documents and Settings\Ceonn>echo %TCL_LIBRARY%
C:\IBMTOOLS\Python22\tcl\tcl8.4
 
C:\Documents and Settings\Ceonn>echo %TK_LIBRARY%
C:\IBMTOOLS\Python22\tcl\tk8.4
 
C:\Documents and Settings\Ceonn>echo %PYTHONPATH%
C:\IBMTOOLS\utils\support;C:\IBMTOOLS\utils\logger
 


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


Re: IDLE doesn't start

2011-03-14 Thread Gennadiy Zlobin
Hi Ceonn,

Well it is obvious that these environment variables are pointing to your old
Python 2.2 installation that is located in
the C:\IBMTOOLS\Python22\tcl\tcl8.4

First, try to delete these variables and find out are they necessary to run
IDLE.
Type in console
set TCL_LIBRARY =
set TK_LIBRARY =
set PYTHONPATH =

Then try to run IDLE. If it will run ok, then you don't need these
variables, you should delete them permamently from your computer.
Please take a look at this article
https://www-304.ibm.com/support/docview.wss?uid=swg21105412

Otherwise if IDLE won't start correctly, try to set TCL_LIBRARY and
TK_LIBRARY to paths to your current Python installation. I suppose it will
be something like C:\\tcl\tcl

BTW if I were you, I would repeat in the same thread because other users
will not understand what discussion you are continuing.

Cheers,
- Gennadiy 


On Sat, Mar 12, 2011 at 4:29 PM, Ceonn Bobst  wrote:

> Hi Gennadiy,
>
> Here is the output I get:
> C:\Documents and Settings\Ceonn>echo %TCL_LIBRARY%
> C:\IBMTOOLS\Python22\tcl\tcl8.4
>
> C:\Documents and Settings\Ceonn>echo %TK_LIBRARY%
> C:\IBMTOOLS\Python22\tcl\tk8.4
>
> C:\Documents and Settings\Ceonn>echo %PYTHONPATH%
> C:\IBMTOOLS\utils\support;C:\IBMTOOLS\utils\logger
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Get Path of current Script

2011-03-14 Thread Alexander Schatten
Hi,

could someone help me with a small problem? I wrote a Python script
that does some RegEx... transformations. Now, this script loads some
configuration data from a file located in the same directory:

open ('config.txt', 'r').

However, this only works when I execute the script being in the
directory where the script is locates, because otherwise, of course,
this config file is not found, as the path is relative. Now my
question: is there an easy way (API) to get the directory of the
currently running script? Something along the line of:

open (API.getCurrentPath + 'config.txt', 'r').


thanks a lot,

cheers

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


Re: Get Path of current Script

2011-03-14 Thread Gennadiy Zlobin
To get the directory of the current running script try:

import os.path
path = os.path.dirname(__file__)



- Gennadiy 


On Mon, Mar 14, 2011 at 3:25 PM, Alexander Schatten wrote:

> Hi,
>
> could someone help me with a small problem? I wrote a Python script
> that does some RegEx... transformations. Now, this script loads some
> configuration data from a file located in the same directory:
>
> open ('config.txt', 'r').
>
> However, this only works when I execute the script being in the
> directory where the script is locates, because otherwise, of course,
> this config file is not found, as the path is relative. Now my
> question: is there an easy way (API) to get the directory of the
> currently running script? Something along the line of:
>
> open (API.getCurrentPath + 'config.txt', 'r').
>
>
> thanks a lot,
>
> cheers
>
> Alex
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Steven D'Aprano
On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:

> is there an easy way (API) to get the directory of the currently running
> script? 

import __main__
import os
print os.path.dirname(__main__.__file__)


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


Re: Get Path of current Script

2011-03-14 Thread Alain Ketterlin
Alexander Schatten  writes:

> could someone help me with a small problem? I wrote a Python script
> that does some RegEx... transformations. Now, this script loads some
> configuration data from a file located in the same directory:

sys.path[0] is the path to the directory containing the script that the
interpreter started with.

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


Re: Get Path of current Script

2011-03-14 Thread Duncan Booth
Steven D'Aprano  wrote:

> On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
> 
>> is there an easy way (API) to get the directory of the currently running
>> script? 
> 
> import __main__
> import os
> print os.path.dirname(__main__.__file__)
> 
That code is pretty version specific:

In older versions of Python you may have to convert __main__.__file__ to an 
absolute path (I'm not sure what 2.6 does as I don't have it to hand, but 
2.5 and earlier could have a relative path).

In Python 3.x the print will break.

Also, any version can also fail if the code using it is called from an 
interactive session as __main__ doesn't always have a __file__ attribute.

So for the paranoid you might use:

  print(os.path.dirname(os.path.abspath(
getattr(__main__,'__file__','__main__.py'

which falls back to giving you the current directory from a script (and 
might be what you want if you haven't changed it since the script started).

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


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread Jean-Michel Pichavant

bukzor wrote:

We've been doing a fair amount of Python scripting, and now we have a
directory with almost a hundred loosely related scripts. It's
obviously time to organize this, but there's a problem. These scripts
import freely from each other and although code reuse is  generally a
good thing it makes it quite complicated to organize them into
directories.

There's a few things that you should know about our corporate
environment:

1) I don't have access to the users' environment. Editing the
PYTHONPATH is out, unless it happens in the script itself.
2) Users don't install things. Systems are expected to be *already*
installed and working, so setup.py is not a solution.

I'm quite willing to edit my import statements and do some minor
refactoring, but the solutions I see currently require me to divide
all the code strictly between "user runnable scripts" and "libraries",
which isn't feasible, considering the amount of code.

Has anyone out there solved a similar problem? Are you happy with it?
--Buck
  

Short story:
   - Too late ! haha

Long story:
   - Windows: no clue
   - Unix like:
  * There was a time I didn't have root access to my machine. In 
that case what most people do is create a ~/bin and ~/lib directory 
where libraries and executable will be placed in. Works for everything 
including python and most installers have a --prefix options to change 
the installation root directory. I have a ~/lib/python2.5/site-packages 
for instance where python package are installed, and I don't need root 
access to install official packages.
  * Ask your IT to install on every user python site a symbolic 
link to a network directory where you'll install your package


I'm sopping here 'cause from your OP, I have the feeling it's a locked 
Windows environment (troll free statement).


JM

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


Re: Get Path of current Script

2011-03-14 Thread Gennadiy Zlobin
I tested my solution on python 2.5, 2.8 and 3.1 and everything seems working
well



- Gennadiy 


On Mon, Mar 14, 2011 at 4:29 PM, Duncan Booth
wrote:

> Steven D'Aprano  wrote:
>
> > On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
> >
> >> is there an easy way (API) to get the directory of the currently running
> >> script?
> >
> > import __main__
> > import os
> > print os.path.dirname(__main__.__file__)
> >
> That code is pretty version specific:
>
> In older versions of Python you may have to convert __main__.__file__ to an
> absolute path (I'm not sure what 2.6 does as I don't have it to hand, but
> 2.5 and earlier could have a relative path).
>
> In Python 3.x the print will break.
>
> Also, any version can also fail if the code using it is called from an
> interactive session as __main__ doesn't always have a __file__ attribute.
>
> So for the paranoid you might use:
>
>  print(os.path.dirname(os.path.abspath(
>getattr(__main__,'__file__','__main__.py'
>
> which falls back to giving you the current directory from a script (and
> might be what you want if you haven't changed it since the script started).
>
> --
> Duncan Booth http://kupuguy.blogspot.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compile time evaluation of dictionaries

2011-03-14 Thread Jean-Michel Pichavant

Gerald Britton wrote:

Today I noticed that an expression like this:

"one:%(one)s two:%(two)s" % {"one": "is the loneliest number", "two":
"can be as bad as one"}

could be evaluated at compile time, but is not:

  

dis(compile(


... '"one:%(one)s two:%(two)s" % {"one": "is the loneliest number",
"two": "can be as bad as one"}',
... '','exec'))
 1   0 LOAD_CONST   0 ('one:%(one)s two:%(two)s')
 3 BUILD_MAP2
 6 LOAD_CONST   1 ('is the loneliest number')
 9 LOAD_CONST   2 ('one')
12 STORE_MAP
13 LOAD_CONST   3 ('can be as bad as one')
16 LOAD_CONST   4 ('two')
19 STORE_MAP
20 BINARY_MODULO
21 POP_TOP
22 LOAD_CONST   5 (None)
25 RETURN_VALUE
  


Any idea why Python works this way?  I see that, in 3.2, an
optimization was done for sets (See "Optimizations" at
http://docs.python.org/py3k/whatsnew/3.2.html) though I do not see
anything similar for dictionaries.

--
Gerald Britton



  

1/ because no one would ever see the difference.
2/ immutables can always be evaluated before any high CPU consuming loop
3/ it would make the implementation more complex (i.e. more work for our 
beloved active community) for no gain
4/ you can write C code to speed up things: 
http://docs.python.org/extending/extending.html, when really needed.


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


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Jean-Michel Pichavant

Steven D'Aprano wrote:
The removal of cmp from the sort method of lists is probably the most 
disliked change in Python 3. On the python-dev mailing list at the 
moment, Guido is considering whether or not it was a mistake.


If anyone has any use-cases for sorting with a comparison function that 
either can't be written using a key function, or that perform really 
badly when done so, this would be a good time to speak up.




  

You seem concerned by this removal, do you have any use-case ?

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


Re: Get Path of current Script

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 5:53:25 AM UTC-4, Alain Ketterlin wrote:

> sys.path[0] is the path to the directory containing the script that the
> interpreter started with.

How about os.path.dirname(os.path.realpath(sys.argv[0]))? I think realpath is 
required in case someone runs it from a symlink. I don't know what can be done 
about Windows symlinks created with mklink. Thankfully that's uncommon.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Alexander Schatten
Thanks for the comments so far. This sounds to be more complicated in
detail than I expected. I wonder how all the other Python programs and
scripts are doing that...
-- 
http://mail.python.org/mailman/listinfo/python-list


writing command-line options into file

2011-03-14 Thread hiral
For following example, how to write command-line option OR metavar
into file...

parser.add_option("-opt1", metavar="MY_OPTION1", default=123)
parser.add_option("-opt2", metavar="YOUR_OPTION2" ,default= "abc")
parser.add_option('-opt3", metavar="FLAG", default=True)

do we have any facility to write command-line option OR 'metavar' into
file as follow...

output_file:
MY_OPTION1 123
YOUR_OPTION2 abc
FLAG True

OR
output_file:
opt1 123
opt2 abc
opt3 True

Any idea?

Please note that in actual application there are about 80 to 90
command-line options.

Thank you in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Steven D'Aprano
On Mon, 14 Mar 2011 10:29:42 +, Duncan Booth wrote:

> Steven D'Aprano  wrote:
> 
>> On Mon, 14 Mar 2011 02:25:46 -0700, Alexander Schatten wrote:
>> 
>>> is there an easy way (API) to get the directory of the currently
>>> running script?
>> 
>> import __main__
>> import os
>> print os.path.dirname(__main__.__file__)
>> 
> That code is pretty version specific:
> 
> In older versions of Python you may have to convert __main__.__file__ to
> an absolute path (I'm not sure what 2.6 does as I don't have it to hand,
> but 2.5 and earlier could have a relative path).

Ah, so it does -- it looks like __file__ will have an absolute path only 
if you call the script with an absolute path, otherwise it will have a 
relative path. I've tested that with all versions from 2.2 to 2.7. 
Version 2.2 gives an AttributeError when looking up __file__, so I assume 
there's no point going back any further.

In any case, would it matter? Whether absolute or relative, the main 
thing is that the directory name given should be sufficient for the 
script to discover its own location, so as to locate its data files. A 
relative path should work for that, so long as the script doesn't change 
its own working directory.


> In Python 3.x the print will break.

True. But if you're going to be that pedantic, I should have saved the 
directory name to a variable, rather than printing it :P


> Also, any version can also fail if the code using it is called from an
> interactive session as __main__ doesn't always have a __file__
> attribute.

Yes, but then it's not a script, is it? :)


> So for the paranoid you might use:
> 
>   print(os.path.dirname(os.path.abspath(
> getattr(__main__,'__file__','__main__.py'
> 
> which falls back to giving you the current directory from a script (and
> might be what you want if you haven't changed it since the script
> started).

The truly paranoid might prefer to use './__main__.py' instead of a bare 
file name, and avoid dealing with the empty string.



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


Re: writing command-line options into file

2011-03-14 Thread Brian Blais

On Mar 14, 2011, at 8:13 AM, hiral wrote:

> For following example, how to write command-line option OR metavar
> into file...
> 
> parser.add_option("-opt1", metavar="MY_OPTION1", default=123)
> parser.add_option("-opt2", metavar="YOUR_OPTION2" ,default= "abc")
> parser.add_option('-opt3", metavar="FLAG", default=True)
> 
> do we have any facility to write command-line option OR 'metavar' into
> file as follow...
> 
> output_file:
> MY_OPTION1 123
> YOUR_OPTION2 abc
> FLAG True
> 
> OR
> output_file:
> opt1 123
> opt2 abc
> opt3 True
> 

Perhaps something like this would work.  This loads and saves just the default 
values.  A little more digging could make it more robust, and inclusive.  The 
output file (test.yaml) looks like:

opt1: 123
opt2: abc
opt3: true



from __future__ import with_statement
from optparse import OptionParser
import yaml

class MyOptionParser(OptionParser):

def save(self,fname):
data=self.defaults
with open(fname,'w') as fid:
yaml.dump(data,fid,default_flow_style=False)

def load(self,fname):
data=yaml.load(open(fname))
for option in data:
self.add_option("--"+option,default=data[option])



parser = MyOptionParser()

parser.add_option("--opt1", metavar="MY_OPTION1", default=123)
parser.add_option("--opt2", metavar="YOUR_OPTION2" ,default= "abc")
parser.add_option("--opt3", metavar="FLAG", default=True)


parser.save('test.yaml')


newparser=MyOptionParser()
newparser.load('test.yaml')


bb

-- 
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Steven D'Aprano
On Mon, 14 Mar 2011 12:10:27 +0100, Jean-Michel Pichavant wrote:

> Steven D'Aprano wrote:
>> The removal of cmp from the sort method of lists is probably the most
>> disliked change in Python 3. On the python-dev mailing list at the
>> moment, Guido is considering whether or not it was a mistake.
>>
>> If anyone has any use-cases for sorting with a comparison function that
>> either can't be written using a key function, or that perform really
>> badly when done so, this would be a good time to speak up.
>>
>>
>>
>>
> You seem concerned by this removal, do you have any use-case ?

You seem concerned by my concern. Why do you think I am concerned?

(1) I'm not concerned, but many people are. If you search the archives of 
this newsgroup (mailing list), you'll see that I have defended the 
removal of cmp from sort, e.g. this post:

http://www.mail-archive.com/python-list%40python.org/msg261728.html


(2) If I had a good use-case for keeping cmp, I wouldn't need to ask 
others if they had a good use-case.

As it is, Guido himself has mentioned one such good use for a comparison 
function when sorting. Use of a key function trades off memory for time, 
while sorting with a comparison function makes the opposite trade off, 
using more time for the sake of saving memory. 



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


Re: Get Path of current Script

2011-03-14 Thread Ulrich Eckhardt
Alexander Schatten wrote:
> Thanks for the comments so far. This sounds to be more complicated in
> detail than I expected. I wonder how all the other Python programs and
> scripts are doing that...

Well, it's not like that's impossible to find out, the source is out
there! :)

Anyhow, you basically have two variants AFAIK:
1. Unix variant
Here, you have the program in the bin directory (e.g. /usr/bin/foo) and the
required data files in a library directory (e.g. /usr/lib/foo/data). The
location of the latter is fixed when the program is installed, i.e. the
path is coded as a placeholder in the source and then replaced according to
the installation-path.

2. Windows variant
Here, the program is installed completely in a folder (e.g. C:\Program
Files\foo) where both the program itself and any other data files are
installed. The special folder (e.g. C:\Program Files) where programs are
stored can be retrieved from the OS, the program-specific path that follows
is usually hard-coded.


Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: writing command-line options into file

2011-03-14 Thread Steven D'Aprano
On Mon, 14 Mar 2011 05:13:37 -0700, hiral wrote:

> For following example, how to write command-line option OR metavar into
> file...
> 
> parser.add_option("-opt1", metavar="MY_OPTION1", default=123)
> parser.add_option("-opt2", metavar="YOUR_OPTION2" ,default= "abc")
> parser.add_option('-opt3", metavar="FLAG", default=True)


What is parser?


> do we have any facility to write command-line option OR 'metavar' into
> file as follow...
> 
> output_file:
> MY_OPTION1 123
> YOUR_OPTION2 abc
> FLAG True

Write a helper function like this:

def opt_writer(f, parser, *args, **kwargs):
parser.add_option(*args, **kwargs)
template = "%(metavar)s %(default)s\n"
f.write(template % kwargs)

then call it:

ofile = open('output.txt', 'w')
opt_writer(ofile, parser, "-opt1", metavar="MY_OPTION1", default=123)
opt_writer(ofile, parser, "-opt2", metavar="YOUR_OPTION2" ,default= "abc")
opt_writer(ofile, parser, "-opt3", metavar="FLAG", default=True)
ofile.close()


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


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Jean-Michel Pichavant

Steven D'Aprano wrote:

On Mon, 14 Mar 2011 12:10:27 +0100, Jean-Michel Pichavant wrote:

  

Steven D'Aprano wrote:


The removal of cmp from the sort method of lists is probably the most
disliked change in Python 3. On the python-dev mailing list at the
moment, Guido is considering whether or not it was a mistake.

If anyone has any use-cases for sorting with a comparison function that
either can't be written using a key function, or that perform really
badly when done so, this would be a good time to speak up.




  

You seem concerned by this removal, do you have any use-case ?



You seem concerned by my concern. Why do you think I am concerned?

(1) I'm not concerned, but many people are. If you search the archives of 
this newsgroup (mailing list), you'll see that I have defended the 
removal of cmp from sort, e.g. this post:


http://www.mail-archive.com/python-list%40python.org/msg261728.html


(2) If I had a good use-case for keeping cmp, I wouldn't need to ask 
others if they had a good use-case.


As it is, Guido himself has mentioned one such good use for a comparison 
function when sorting. Use of a key function trades off memory for time, 
while sorting with a comparison function makes the opposite trade off, 
using more time for the sake of saving memory. 

  
Just to know if currently the use-case count is zero. Nothing evil 
hidden behind my question :p


JM



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


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 2:38:50 AM UTC-4, bukzor wrote:

> I've written this many times. It has issues. In fact, I've created a
> library for this purpose, for the following reasons.

If you're linking to a common file, couldn't you just add in the base folder 
there? I don't think it's a bad practice to hard-code an absolute path in a 
single file. If the path changes you only have to update one line. For example:

# script.py
import _path   # _path.py is a symbolic link

# _path.py:
base = '/absolute/path/to/base'
import site
site.addsitedir(base) 

This also adds paths from any .pth files located in the base folder, but that 
shouldn't be necessary if all of the scripts are located within one package and 
sub-packages. 

On a Posix-compliant system, you should be able to use the following to avoid 
hard-coding the path (untested, though):

# _path.py:
import os.path
import site
base = os.path.dirname(os.path.realpath(__file__))
site.addsitedir(base) 

Possibly it has to be abspath(realpath(__file__)), but the docs say realpath 
returns a 'canonical format', which should be the absolute path. I can't check 
right now, and this doesn't work for me at all on Windows. Python can't resolve 
the symbolic links created by mklink to the real path, which is odd since 
mklink has been standard in Windows for many years now. Maybe Python 3.x 
handles it better.
-- 
http://mail.python.org/mailman/listinfo/python-list


Compile time evaluation of dictionaries

2011-03-14 Thread Gerald Britton
Jean-Michel Pichavan wrote:
>> Today I noticed that an expression like this:
>>
>> "one:%(one)s two:%(two)s" % {"one": "is the loneliest number", "two":
>> "can be as bad as one"}
>>
>> could be evaluated at compile time, but is not:
>>
>>
[snip]
>> Any idea why Python works this way?  I see that, in 3.2, an
>> optimization was done for sets (See "Optimizations" at
>> http://docs.python.org/py3k/whatsnew/3.2.html) though I do not see
>> anything similar for dictionaries.

>
>1/ because no one would ever see the difference.

The same thing could be said about sets, yet a similar optimization
was added to 3.2

>2/ immutables can always be evaluated before any high CPU consuming loop

immutables could also be evaluated at compile time, which would
obviate any such concern.

>3/ it would make the implementation more complex (i.e. more work for our
>beloved active community) for no gain

See my reply to 1/ above.

>4/ you can write C code to speed up things:
>http://docs.python.org/extending/extending.html, when really needed.

How do you spell red herring?

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


RE: Inserting record into postgresql database

2011-03-14 Thread Meszaros, Stacy
After much experimentation, I found the following solution:

commandString = 
"\\xA500046898000AC73D228B0B018040003BF9"
insert = "INSERT INTO profile_commands(command_id, profile_id, 
execution_order, command, command_type) VALUES(%s, %s, 0, %s, 1); "
self.cur.execute(insert, (command_id, profileID, commandString,))


The 'key' to solving this was to add the \\x to the beginning of commandString.

Thanks to all who pondered this

Stacy

From: MRAB [pyt...@mrabarnett.plus.com]
Sent: Friday, March 11, 2011 7:32 PM
To: python-list@python.org
Subject: Re: Inserting record into postgresql database

On 11/03/2011 22:08, Meszaros, Stacy wrote:
> Hello all,
>
> I am using python 2.6 and the psycopg2 module for the postgres connection
> The following code is supposed to insert a record into a table with a bytea 
> field. (bytearray)
> I am having difficulty getting to field inserted properly.  The snippet below 
> inserts the first 8 bit hex value, which is 0xA5, correctly I believe - as 
> \245
> Not sure why, but the rest of the 'string' is not inserted into the db table.
> Any ideas?
>
>  commandString = 
> re.escape("A500046898000AC73D228B0B018040003BF9")
>  insert = "INSERT INTO profile_commands(command_id, profile_id, 
> execution_order, command, command_type) VALUES(%s, %s, 0, %s, 1); "
>  self.cur.execute(insert, (command_id, profileID,commandString,))
>  self.conn.commit()
>
> Thanks for any ideas you can give me
> Stacy
>
Does this help?

http://stackoverflow.com/questions/3103242/inserting-text-string-with-hex-into-postgresql-as-a-bytea

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


Re: Get Path of current Script

2011-03-14 Thread Grant Edwards
On 2011-03-14, Alexander Schatten  wrote:

> Thanks for the comments so far. This sounds to be more complicated in
> detail than I expected. I wonder how all the other Python programs and
> scripts are doing that...

The usual answer is "they don't".

At least in the Unix world, finding out the path to the running
program is almost always the wrong way to solve the problem.  However,
since we don't know the problem that's being solved, it's hard to
suggest the "right" way to solve it.

-- 
Grant Edwards   grant.b.edwardsYow! ... My pants just went
  at   on a wild rampage through a
  gmail.comLong Island Bowling Alley!!
-- 
http://mail.python.org/mailman/listinfo/python-list


function annotations in open source projects

2011-03-14 Thread Filip Gruszczyński
Do you know any open source python3 projects that use function
annotations? I would like to see some real use, so maybe I find them
useful to use in my own project.

-- 
Filip Gruszczyński
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function annotations in open source projects

2011-03-14 Thread Daniel Urban
> Do you know any open source python3 projects that use function
> annotations? I would like to see some real use, so maybe I find them
> useful to use in my own project.

threecheck uses them for type checking: http://pypi.python.org/pypi/threecheck


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


cProfile taking up too much memory?

2011-03-14 Thread Dysgraphic Programmer
I am attempting to profile my project in python, but I am running out
of memory.

My project itself is fairly memory intensive, but even half-size runs
are dieing with "MemoryError" when run under cProfile.

Doing smaller runs is not a good option, because we suspect that the
run time is scaling super-linearly, and we are trying to discover
which functions are dominating during large runs.

Why is cProfile taking so much memory? Can I make it take less? Is
this normal?
-- 
http://mail.python.org/mailman/listinfo/python-list


function annotations in open source projects

2011-03-14 Thread geremy condra
I use them in evpy to automatically wrap c functions with a decorated,
annotated function. The decorator code is also up on aspn, just search for
"c function decorator" and it should come up. I did a similar thing with
java a few years ago as well.

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


Re: function annotations in open source projects

2011-03-14 Thread Patty

  - Original Message - 
  From: geremy condra 
  To: Filip Gruszczynski 
  Cc: python-list@python.org 
  Sent: Monday, March 14, 2011 10:37 AM
  Subject: function annotations in open source projects


  I use them in evpy to automatically wrap c functions with a decorated, 
annotated function. The decorator code is also up on aspn, just search for "c 
function decorator" and it should come up. I did a similar thing with java a 
few years ago as well.

  Geremy Condra



--


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


  Hello Geremy - I would like to see this.  I couldn't see in the other 
reference, "threecheck uses them for type checking: 
http://pypi.python.org/pypi/threecheck"; from Daniels message where to start 
looking on their site what the code actually looks like.  I googled "c function 
decorator" but it looks like you have this on something called aspn?  What is 
that link??
  Thanks

  Patty


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


Re: Get Path of current Script

2011-03-14 Thread Alexander Schatten
They don't. Hm, ok, I am always for best practices. If there is a
better way to do it I am open for suggestions ;-) How would the best
practice be to load configuration data from a file.

I mean, this is something very common: you write a program or a script
and want to load some configuration data.


thanks


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


Re: Get Path of current Script

2011-03-14 Thread Jason Swails
On Mon, Mar 14, 2011 at 11:25 AM, Alexander Schatten wrote:

> They don't. Hm, ok, I am always for best practices. If there is a
> better way to do it I am open for suggestions ;-) How would the best
> practice be to load configuration data from a file.
>
> I mean, this is something very common: you write a program or a script
> and want to load some configuration data.
>

For *nix, many utilities publish conf files in the user's home directory in
some sort of .conf file.  That makes it easy to give each user their own
.conf file (if multiple users will use it), and avoids any kind of
permission issues that arise if your script is in a folder whose write
positions are turned off.  It's also where common resource files are loaded
(same kind of idea).

Examples: ~/.bashrc, ~/.vimrc, ~/.bash_profile, ~/.cshrc, etc.

Alternatives are hard-coding the install directory location as part of the
install process, which is done sometimes as well.  This is easily accessible
from python via

os.environ['HOME']

or

os.getenv('HOME')

All the best,
Jason

-- 
Jason M. Swails
Quantum Theory Project,
University of Florida
Ph.D. Candidate
352-392-4032
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Santoso Wijaya
You can make it a required input with default values to sys.argv when you
start the program. Parse the content of the file given by sys.argv, then
cache it in some global module or pass it around as arguments...

That said, if you still want to go the route of knowing where the current
directory of a given script is, I would do:

import os

HERE = os.path.dirname(os.path.abspath(__file__))
CFGFILE = os.path.join(HERE, 'config.cfg')

~/santa


On Mon, Mar 14, 2011 at 11:25 AM, Alexander Schatten wrote:

> They don't. Hm, ok, I am always for best practices. If there is a
> better way to do it I am open for suggestions ;-) How would the best
> practice be to load configuration data from a file.
>
> I mean, this is something very common: you write a program or a script
> and want to load some configuration data.
>
>
> thanks
>
>
> Alex
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Paul Rubin
Steven D'Aprano  writes:
> If anyone has any use-cases for sorting with a comparison function that 
> either can't be written using a key function, or that perform really 
> badly when done so, this would be a good time to speak up.

We've had this discussion a couple times before.  I remember an example
involving comparing tree structures, that I thought couldn't be done
with key= in a reasonable way, and that this was a reasonable real-world
example.  Raymond H then gave a way of encoding it with key= which
looked ok at the time, but I decided sometime later that I think we both
missed an error in that encoding, so I've been wanting to dig it out
again and look more closely.

key= can of course burn a lot more memory because of the DSU pattern
(say you are sorting file handles according to the contents of the file,
so with key= you might have to read N multi-megabyte files where with
cmp you just pairwise-compare them until they differ, which is probably
in the first few bytes).

Finally I concocted an "infinite" example which we agreed is artificial:
you are given a list of generators denoting real numbers, for example
pi generates the infinite sequence 3,1,4,1,5,9... while e generates 
2,7,1,8,...  You can sort these with cmp but not with key.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How should I handle socket receiving?

2011-03-14 Thread Hans
On Mar 12, 10:13 pm, Tim Roberts  wrote:
> Hans  wrote:
>
> >I'm thinking to write a code which to:
> >1. establish tons of udp/tcp connections to a server
>
> What does "tons" mean?  Tens?  Hundreds?
>
> >my question is how should I handle receiving traffic from each
> >connection respectively?
>
> You're really going to want to use "select".  You can store the objects in
> a dictionary where the key is the socket number.  That way, you can use the
> result of the select and get your network object directly.
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.

I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
my_socket=socket_thread.socket_thread(i,host,port)
my_socket.send(some_data)
my_socket.recv()

socket_thread.py
class socket_thread:
def __init__:
  self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
def send(data):
  self.soc_handle.send(data)
def recv():
  while 1:
 
input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
  data=input_list[0].recv(2048)
  print data

But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 9:45:51 AM UTC-4, eryksun () wrote:
> 
> If you're linking to a common file, couldn't you just add in 
> the base folder there? 
>
> ...
> 
> # script.py
> import _path   # _path.py is a symbolic link
> 
> # _path.py:
> base = '/absolute/path/to/base'
> import site
> site.addsitedir(base) 

To be clear on the file structure, I'm picturing that 'base' is a path on each 
user's shell path where all the accessible scripts are linked, and that this is 
also the package directory. So when a linked script runs "import _path" it will 
import the _path.py that's located in base, which adds the base path to 
Python's sys.path. On the other hand, any subsequently imported modules will be 
found by searching sys.path. Thus each subdirectory needs the additional 
symbolic link back to the base _path.py. It's a bit convoluted, but so are the 
constraints of this problem -- at least to me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread John Gordon
In <5520ec67-bc4e-4f81-b27a-cf1f8c8de...@v11g2000prb.googlegroups.com> 
Alexander Schatten  writes:

> I mean, this is something very common: you write a program or a script
> and want to load some configuration data.

There are several good ways to do it:

+ Assume the config file is in the current directory (crude, but it can
  work)

+ Put the config file in a known location (i.e. /etc/foorc, ~/.foorc)

+ Put the location of the config file in an environment variable

+ As part of your application's install process, ask the user where the
  config file will be stored, and record their answer

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread bruce bushby
I'm probably going off on a tangent here..but has anybody seen any
efforts to allow python to "import modules via a socket" ...rather
then just a "dir path" ?





On Mon, Mar 14, 2011 at 7:56 PM, eryksun ()  wrote:

> On Monday, March 14, 2011 9:45:51 AM UTC-4, eryksun () wrote:
> >
> > If you're linking to a common file, couldn't you just add in
> > the base folder there?
> >
> > ...
> >
> > # script.py
> > import _path   # _path.py is a symbolic link
> >
> > # _path.py:
> > base = '/absolute/path/to/base'
> > import site
> > site.addsitedir(base)
>
> To be clear on the file structure, I'm picturing that 'base' is a path on
> each user's shell path where all the accessible scripts are linked, and that
> this is also the package directory. So when a linked script runs "import
> _path" it will import the _path.py that's located in base, which adds the
> base path to Python's sys.path. On the other hand, any subsequently imported
> modules will be found by searching sys.path. Thus each subdirectory needs
> the additional symbolic link back to the base _path.py. It's a bit
> convoluted, but so are the constraints of this problem -- at least to me.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How should I handle socket receiving?

2011-03-14 Thread MRAB

On 14/03/2011 19:47, Hans wrote:

On Mar 12, 10:13 pm, Tim Roberts  wrote:

Hans  wrote:


I'm thinking to write a code which to:
1. establish tons of udp/tcp connections to a server


What does "tons" mean?  Tens?  Hundreds?


my question is how should I handle receiving traffic from each
connection respectively?


You're really going to want to use "select".  You can store the objects in
a dictionary where the key is the socket number.  That way, you can use the
result of the select and get your network object directly.
--
Tim Roberts, t...@probo.com
Providenza&  Boekelheide, Inc.


I wrote code like this:
main proc:
import socket_thread
#start 1000 connection
while i<1000:
 my_socket=socket_thread.socket_thread(i,host,port)
 my_socket.send(some_data)
 my_socket.recv()


This won't run as-is because you never assign to "i".


socket_thread.py
class socket_thread:
 def __init__:
   self.soc_handle=socket.socket(socket.IF_INET,socket.DGRAM)
 def send(data):
   self.soc_handle.send(data)
 def recv():
   while 1:

input_list,output_list,exec_list=select.select([self.soc_handle],[],[],
2)
   data=input_list[0].recv(2048)
   print data

But it does not work as I hope. main proc can only initiate one thread
and then trapped by it, cannot get out.
I'm sure I missed something but I don't know. Thanks for any help.


Your "socket_thread" class is just a normal class. You create an
instance, use it to send data, and then call its "recv" method, which
loops forever.
--
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 3:56:15 PM UTC-4, eryksun () wrote:
> To be clear on the file structure, I'm picturing that 'base' is a 
> path on each user's shell path where all the accessible scripts 
> are linked, and that this is also the package directory. 

Wait, this won't work when the script is linked to from somewhere else, which 
means the code still has to be based on __file__ or sys.argv[0] or sys.path[0], 
and have to get the absolute/real path in case it's a link. 

Along those lines, you (bukzor) wrote that

> What I do right now is to symlink this library to all script 
> directories to allow them to bootstrap and gain access to 
> libraries not in the local directory.

Won't this also fail if it's running from a link? The link to the library won't 
necessarily be in the current directory.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get Path of current Script

2011-03-14 Thread Grant Edwards
On 2011-03-14, Alexander Schatten  wrote:

> They don't. Hm, ok, I am always for best practices. If there is a
> better way to do it I am open for suggestions ;-) How would the best
> practice be to load configuration data from a file.
>
> I mean, this is something very common: you write a program or a
> script and want to load some configuration data.

Indeed that is very common, and there's been a "standard" way to do
that since before dirt.

The standard on Unix is to look in the following places in (this
order), and use the first one you find:

 1) The location specified by a command line option (-f or -c is
common).

 2) The location specified by an environment variable like
MYPROGNAME_CONFIG.

 3) The current directory (usually a "hidden" file name like
.myprognamerc or .myprog.config)

 4) The current user's home directory -- same file name as 3).

 5) The "configuration directory".  Each distro has a spec for where
that is, but it's usually _not_ a hidden file name, and is
somemthing like /etc/myprogname.conf or /etc/myprognamerc.  For
locally-installed stuff, it's usually /usr/local/etc/muyprog.conf
or something like that.

Notice that it has nothing to do with the location of the program's
executable.

Not all Unix apps look in all 5 places (#2, is probably not quite as
universal as the rest).

Some apps have a directory of config files instead of a single file,
but the general plan is the same.

-- 
Grant Edwards   grant.b.edwardsYow! I smell like a wet
  at   reducing clinic on Columbus
  gmail.comDay!
-- 
http://mail.python.org/mailman/listinfo/python-list


Dynamic loading of module with explicit file path

2011-03-14 Thread Tim Johnson
FYI: Using python 2.6 on ubuntu 10, backward compatibilty to 2~
needed. Self-employed programmer 24 years. Python 9 years,
part-time.

I'm not addressing an existing problem, but looking for ideas that
might help me to do some upgrading (if needed). 

I'd like to solicit comments on the following methods for
dynamically loading a module from an explicit file path.
Code is all untested!

## 1 :: Go there and get it.
import sys,os
def my_import(module_name,path):
cwd = os.getcwd() 
os.chdir(path)
if sys.path[0] != "":
sys.path.insert(0,"")
module = __import__(module_name)
os.chdir(cwd)
return module
## 2 :: temporarily modify sys.path, no dir change
import sys
def my_import(module_name,path):
sys_path = sys.path
sys.path.insert(0,path)
module = __import__(module_name)
sys.path = sys_path
return module
## 3 :: use the imp module
import imp
def my_import(module_name,path):
fp, pathname, description = imp.find_module(module_name,[path])
module = imp.load_module(module_name, fp, pathname, description)
return module

TIA
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compile time evaluation of dictionaries

2011-03-14 Thread Terry Reedy

On 3/14/2011 10:21 AM, Gerald Britton wrote:


Any idea why Python works this way?  I see that, in 3.2, an
optimization was done for sets (See "Optimizations" at
http://docs.python.org/py3k/whatsnew/3.2.html) though I do not see
anything similar for dictionaries.




1/ because no one would ever see the difference.


The same thing could be said about sets, yet a similar optimization
was added to 3.2


For one and only one context.


2/ immutables can always be evaluated before any high CPU consuming loop


immutables could also be evaluated at compile time, which would
obviate any such concern.


3/ it would make the implementation more complex (i.e. more work for our
beloved active community) for no gain


See my reply to 1/ above.


You are missing the point. *Every* optimization has a cost for 
*everyone* (the test to see whether it should be applied or not. It has 
a gain for a few -- those for whom the test is true. Do those gains 
outweigh the cost?


In the particular case of 'ob in constant_set', people were avoiding 
that and instead writing 'ob in constant_tuple' because of the cost of 
recreating the set (but not the tuple) each time the expression is 
evaluated. Note that frozenset(constant_set) is even worse because 
*that* is not optimized either. If the set is very big, the hash lookup 
is faster than a linear scan of a tuple. So the cost-benefit decision 
was 'yes'.


In your example
  "one:%(one)s two:%(two)s" % {"one": "is the loneliest number",
  "two":"can be as bad as one"}
can be instead put into the code as
   'one:is the loneliest number two:can be as bad as one'
which is easier to write and read. So there is little need if any to 
write the expression. Hence little benefit and the cost-benefit decision 
for most will be 'no'.


The other thing you are missing is that the peephole optimizer is hard 
code and easily broken when changed. The folding of '-' and int literals 
somehow got disabled some time ago and was just recently fixed again. 
Even worse are changes that produce bugs, as seems to happen regularly 
with more agressive optimizers. Attempts to compile CPython with gcc 
with all 'optimizations' turned on has demonstrated that well enough. 
The prime author of Python's bytecode peephole optimizer, Raymond H., 
would like to replace some or all of it with an ast optimizer, rather 
than expand it.


Perhaps if and when that is done, someone will devise some broader but 
guaranteed safe detect and replace rules.


--
Terry Jan Reedy

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


Re: Get Path of current Script

2011-03-14 Thread eryksun ()
On Monday, March 14, 2011 5:17:49 PM UTC-4, Grant Edwards wrote:
>
> Indeed that is very common, and there's been a "standard" way to do
> that since before dirt.
> 
> The standard on Unix is to look in the following places in (this
> order), and use the first one you find:

On Windows it's typical to use the system registry HKLM\Sofwtare and/or 
HKCU\Software, or a system-wide config files in %ALLUSERSPROFILE% (probably 
read-only for unprivileged users) or per-user config in %APPDATA% or 
%LOCALAPPDATA%. Typically all settings and data will stored in a hierarchy 
relative to the latter, such as Publisher\App\{config}.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing class name causes process to 'hang'

2011-03-14 Thread Vinay Sajip
On Mar 13, 11:48 pm, Tim Johnson  wrote:
>  :) I like my logging module, I believe it may have 'anticipated'
>  the 2.7 module. And I can't count on my client's servers to host
>  2.7 for a while.

Perhaps you already know this, but you don't have to use Python 2.7 to
use the standard logging package - that's been available since Python
2.3.

Regards,

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


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Nobody
On Mon, 14 Mar 2011 12:39:35 -0700, Paul Rubin wrote:

> Finally I concocted an "infinite" example which we agreed is artificial:
> you are given a list of generators denoting real numbers, for example
> pi generates the infinite sequence 3,1,4,1,5,9... while e generates 
> 2,7,1,8,...  You can sort these with cmp but not with key.

Is there some restriction on the return type of the key= function? If not,
you can define a class with appropriate comparison methods and have key=
return instances of that class.

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


Re: Problems of Symbol Congestion in Computer Languages

2011-03-14 Thread Jason Swails
> The metric system is defined to such a ridiculous level of precision
> because we have the technology, and the need, to measure things to that
> level of precision. Standards need to be based on something which is
> universal and unchanging.
>

Other systems of measure (for instance, atomic units and the light-year) are
based on physical constants instead of the size of a stick in France.  As
long as these don't change relative to one another, these approaches are
formally equivalent.  We can't be sure that's true, though.  If that's the
case, then we can't possibly keep an unchanging system of measurement.

Not to disagree with Steven, as these arguments are irrelevant in (almost
all) current scientific research; just to pose thoughts.

Food for thought,
Jason
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread Terry Reedy

On 3/14/2011 4:31 PM, bruce bushby wrote:


but has anybody seen any
efforts to allow python to "import modules via a socket"


I do not remember any such thing.

--
Terry Jan Reedy

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


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Steven D'Aprano
On Mon, 14 Mar 2011 23:37:06 +, Nobody wrote:

> On Mon, 14 Mar 2011 12:39:35 -0700, Paul Rubin wrote:
> 
>> Finally I concocted an "infinite" example which we agreed is
>> artificial: you are given a list of generators denoting real numbers,
>> for example pi generates the infinite sequence 3,1,4,1,5,9... while e
>> generates 2,7,1,8,...  You can sort these with cmp but not with key.
> 
> Is there some restriction on the return type of the key= function? If
> not, you can define a class with appropriate comparison methods and have
> key= return instances of that class.

You mean like this?

http://docs.python.org/dev/library/functools.html#functools.cmp_to_key




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


python script to find Installed programs in Uninstall folder in registry

2011-03-14 Thread KishoreRP
I am working on creating a python script to find Installed programs in
Uninstall folder in registry, the script works perfectly fine on 32
bit machines but errors out with a wmi error on 64 bit machines. Am
not able to get hold of a wmi module for python on 64 bit machines.Is
there one at all? Please ignore indentation errors. I found this
script in some forum, apologies for not giving due credit to the
corresponding author
r = wmi.Registry()
result, names = r.EnumKey (hDefKey=HKEY_LOCAL_MACHINE,
sSubKeyName=r"Software\Microsoft\Windows\CurrentVersion\Uninstall")

separator = "*" * 80
keyPath = r"Software\Microsoft\Windows\CurrentVersion\Uninstall"
for subkey in names:
   try:

path = keyPath + "\\" + subkey
key = OpenKey(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS)

try:
temp = QueryValueEx(key, 'DisplayName')
temp1 = QueryValueEx(key, 'DisplayVersion')
temp2 = QueryValueEx(key, 'Publisher')
display = str(temp[0])
display_ver=str(temp1[0])
display_p=str(temp2[0])

print ('Display Name: ' + display + '\nDisplay
version:  ' + display_ver + '\nVendor/Publisher:  ' + display_p
+'\nRegkey: ' + subkey + '\n')

except:
print ('Regkey: ' + subkey + '\n')


except:
fp = StringIO.StringIO()
traceback.print_exc(file=fp)
errorMessage = fp.getvalue()
#error = 'Error for ' + key + '. Message follows:\n' +
errorMessage
#HelperFuncs.LogError(error)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing many python scripts, in a large corporate environment.

2011-03-14 Thread bukzor
On Mar 14, 1:30 pm, "eryksun ()"  wrote:
> On Monday, March 14, 2011 3:56:15 PM UTC-4, eryksun () wrote:
> > To be clear on the file structure, I'm picturing that 'base' is a
> > path on each user's shell path where all the accessible scripts
> > are linked, and that this is also the package directory.
>
> Wait, this won't work when the script is linked to from somewhere else, which 
> means the code still has to be based on __file__ or sys.argv[0] or 
> sys.path[0], and have to get the absolute/real path in case it's a link.
>
> Along those lines, you (bukzor) wrote that
>
> > What I do right now is to symlink this library to all script
> > directories to allow them to bootstrap and gain access to
> > libraries not in the local directory.
>
> Won't this also fail if it's running from a link? The link to the library 
> won't necessarily be in the current directory.

You're right! QQ
Currently it requires either: 1) no symlinks to scripts or 2)
installation of the pathtools to site-packages.

Mostly I came here because I felt this was a real pain with no good
solution, and felt that I must be missing something essential if
nobody else is thinking or talking about it. When looking at google
code search, this kind of code is rampant (below). Is everyone really
happy with this?

sys.path.insert(0,
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)

see: http://www.google.com/codesearch?hl=en&lr=&q=sys%5C.path.*__file__
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Paul Rubin
Nobody  writes:
>> 2,7,1,8,...  You can sort these with cmp but not with key.
>
> Is there some restriction on the return type of the key= function? If not,
> you can define a class with appropriate comparison methods and have key=
> return instances of that class.

Sorry, yeah, of course you can do it that way, but it's bloody ugly and
you're no longer gaining the supposed benefit of the DSU pattern.
-- 
http://mail.python.org/mailman/listinfo/python-list


A Question on URLLIB

2011-03-14 Thread joy99
Dear Group,
I am trying to construct a web based crawler with Python and for that
I am using the URLLIB module, and  by doing
import urllib and then trying with urllib.urlopen("url).
Am I going fine?
If some one can kindly highlight if I am doing any mistake.
Best Regards,
Subhabrata Banerjee.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Question on URLLIB

2011-03-14 Thread Santoso Wijaya
Well, have you run into any problem, expected or otherwise, yet?

~/santa


On Mon, Mar 14, 2011 at 11:10 PM, joy99  wrote:

> Dear Group,
> I am trying to construct a web based crawler with Python and for that
> I am using the URLLIB module, and  by doing
> import urllib and then trying with urllib.urlopen("url).
> Am I going fine?
> If some one can kindly highlight if I am doing any mistake.
> Best Regards,
> Subhabrata Banerjee.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python script to find Installed programs in Uninstall folder in registry

2011-03-14 Thread Tim Roberts
KishoreRP  wrote:
>
>I am working on creating a python script to find Installed programs in
>Uninstall folder in registry, the script works perfectly fine on 32
>bit machines but errors out with a wmi error on 64 bit machines. Am
>not able to get hold of a wmi module for python on 64 bit machines.

There shouldn't be any difference.  What error do you get, exactly?

Also, let me point out that you can access the registry in a couple of
ways, without invoking the overhead of WMI.  I grant you that they are a
bit wordier, but it might get you around this issue.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Guido rethinking removal of cmp from sort method

2011-03-14 Thread Ian Kelly
On Mon, Mar 14, 2011 at 1:39 PM, Paul Rubin  wrote:
> Finally I concocted an "infinite" example which we agreed is artificial:
> you are given a list of generators denoting real numbers, for example
> pi generates the infinite sequence 3,1,4,1,5,9... while e generates
> 2,7,1,8,...  You can sort these with cmp but not with key.

I would think that you can sort them with key as long as none of the
sequences are equal (which would result in an infinite loop using
either method).  Why not this?

_MISSING = object()

@functools.total_ordering
class IteratorKey(object):

def __init__(self, iterable):
self._iterator = iter(iterable)
self._cache = []

def __iter__(self):
# This is not reentrant, but it's
# good enough for this purpose.
for x in self._cache:
yield x
for x in self._iterator:
self._cache.append(x)
yield x

def __lt__(self, other):
for x, y in itertools.izip_longest(self, other, fillvalue=_MISSING):
if x is _MISSING or y is _MISSING:
return x is _MISSING
elif x < y or y < x:
return x < y
return False

def __eq__(self, other):
for x, y in itertools.izip_longest(self, other, fillvalue=_MISSING):
if x is _MISSING or y is _MISSING or x != y:
   return False
return True
-- 
http://mail.python.org/mailman/listinfo/python-list