Re: suid/sudo in python

2009-03-30 Thread Ben Finney
rustom  writes:

> Im trying to write a program that has su permissions for some file
> system tasks and is non-su elsewhere.

On Unix, ‘su’ is a program for switching to a different user; it's not
the name of a particular user. I presume you mean “ … that has root
permissions for tome file system tasks and is non-root elsewhere”.

> This is typically done in C with suid root owned code.

“suid root” refers to setting the program file with a specific mode
flag on the filesystem so the kernel will cause the process to have
the permissions of the owner of the file (rather than the user who ran
the program), and setting the owner of the program file to ‘root’.

It's not specific to C code (it works no matter what language the
program is written in), and is only one of many ways a program can run
as ‘root’. Perhaps the simplest way to run a program as ‘root’ is to
*be* ‘root’ before running the program.

> What is the python paradigm for this kind of thing? (if at all)

The key thing to realise is that, having relinquished privilege, the
same process can't get it back again as easily. So if you need to do
some tasks as a privileged user, do those *very* early and then drop
the privileges for the rest of the life of the process.

Taking this further, you should isolate exactly what tasks need root
privilege into a separate process altogether, and make that process as
well-tested and simple as possible: it should do nothing *but* those
tasks for which it needs root privilege.

-- 
 \  “We tend to scoff at the beliefs of the ancients. But we can't |
  `\scoff at them personally, to their faces, and this is what |
_o__) annoys me.” —Jack Handey |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


SQL Query shows error in program while it function correctly in psql interface

2009-03-30 Thread bijoy
Hi,

Following SQL assaignment is not working in Program.

intable_bookname=SELECT book_name FROM table_book WHERE book_name='name';

where as this works perfectly fine with psql interface

Pls help me on this

Thanks a lot in advance

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
On Mar 29, 11:16 pm, Chris Rebert  wrote:
> 2009/3/29 Scott David Daniels :
>
> > [email protected] wrote:
>
> >> On Mar 29, 9:52 pm, Chris Rebert  wrote:
>
> >>> On Sun, Mar 29, 2009 at 9:18 PM,   wrote:
>
>  ...
>
> >>> ... Also, you shouldn't use `class_ ` as the name of the first argument
> >>> to
> >>> __new__(). Use `cls` instead since that's the conventional name for
> >>> it.
>
> > Actually, according to PEP 8, class_ is the preferred name.
>
> In other contexts where you have a class as a variable, yes, but not
> in the case of classmethods such as this. See the docs for __new__
> itself for example
> (http://docs.python.org/reference/datamodel.html#object.__new__).
>
> >>> My best guess as to what you're trying to do is (completely untested):
> >>> class myclass(long):
> >>>    def __new__(cls, init_val, reg_info):
> >>>        print reg_info.message
> >>>        instance = long.__new__(cls, init_val)
> >>>        instance.reg_info = reg_info
> >>>        return instance
>
> > Normally, these changes are done in the __init__ phase (post-instance
> > creation), so you might go for something like:
>
> I think the whole reason he's using __new__ instead of __init__ is
> because of this tidbit from the aforementioned __new__() docs:
> """
> __new__() is intended mainly to allow subclasses of immutable types
> (like int, str, or tuple) to customize instance creation. It is also
> commonly overridden in custom metaclasses in order to customize class
> creation.
> """
>
> Cheers,
> Chris
>
> --
> I have a blog:http://blog.rebertia.com

It seems like there's no way to do what I'm trying.  I am confined to
Python 2.5.3 for business reasons.

So I want a class ShadowRegister, which just has a value that I can do
get/set bit sel and slice ops.  I got that working with __init__.  It
was subclass from "object".  Then I wanted a RegisterClass that was a
subclass of ShadowRegister, which would read a hardware register
before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
but when I try to print in hex format ('0x016X') it said it required
an int (but the ShadowRegister class had no issues).  Then I was told
instead of using object I could subclass as long (seemed the only
solution for Python 2.5).  Then when I started to want to add my own
init code (like register length in bits), I was told I should use
__new__ instead of __init__.  So but ever since then I've noticed that
my value is not changing from the initially set value.  I'm really
cornfused now.
--
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter questions: behavior of StringVar, etc

2009-03-30 Thread Eric Brunel
Alan G Isaac wrote:
[snip]
> PS If you were also offering an answer to the second question,
> I missed it altogether, but although it is perhaps slightly
> less obvious than with a StringVar, I would ask the same
> basic question of an IntVar: why does it not behave more
> like an int?  E.g., why is ``int(IntVar(value=5))`` an
> error, or at least, why is ``str(IntVar(value=5))`` the name
> of the IntVar instead of '5'?

The string representation of Tkinter objects seems to be a design principle in
this module: it'll always evaluate to the representation this object has at
tcl level. Since a XxxVar is represented by an actual variable at tcl level,
its string representation is the name of this variable. I guess it's quite
easier to build the commands that'll be passed to the tcl interpreter this
way: you don't have to check the type of the objects you handle, but pass them
through str and insert the result directly in the command.

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


Re: i have to change default tab length in pydev

2009-03-30 Thread Dave Angel
Most such problems are caused by mixing tabs and spaces in the same 
file.  Pick one style and be consistent, and you'll be in good shape.


My approach is to always expand tabs.  My tab key simply gets me to a 
convenient column, there never are any tabs in my source files.


(I don't use either notepad++ or pydev, but most editors I have used or 
written can be configured to use spaces)


Coonay wrote:

during last few days, i code python using notepad++ or pydev, the
compiler always complain there is a problem with Indentation,in my
eyes ,there is no Indentation problem at all,because i format the code
to make it comply with python style guide strictly,but after i change
the default tab length ,it works.

why is that?

  

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


Re: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?)

2009-03-30 Thread Hendrik van Rooyen
"Steven D'Aprano"  wrote:


>Oh noes!!! Python will be just like nearly every other language!!!
>
>Including Python. There are already at least thirteen implementations 
>(forks) of Python (although some of these are defunct or unmaintained):
>
>CPython
>Jython
>IronPython
>Python for .NET
>CLPython
>PyPy
>Unladen Swallow
>Python for S60
>PyVM
>Vyper
>RPython
>Stackless Python
>CapPython
>

Its kind of sad to see unladen swallow, which is just
a promise, on the list, while Shedskin, which isn't, 
is ignored.

Does this say something about big corporations
vs the small man?

- Hendrik


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


Can't get a simple TCP program to work

2009-03-30 Thread Zach
The following *extremely* simple script complains that "Socket is not
connected" when I try to call recv. Could anyone provide some quick
guidance?

http://pastebin.com/m64317b32
--
http://mail.python.org/mailman/listinfo/python-list


Improve module performance by reducing disk reads

2009-03-30 Thread kian tern
Hi all,

I'm writing in Python for about 2 weeks (moved from Perl)
I've ported one of my modules which is a parser for a binary format (see
link bellow for the format specs)

http://etidweb.tamu.edu/cdrom0/image/stdf/spec.pdf

In the first version of the parser I was reading exactly the amount of data
I need to parse
For example 4 bytes per each header
The STDF files tend to be about 40+ MB size with 100K+ records, so I had at
least 1 disk read per record, sometimes 2.

Obviously it's not an efficient way to do it.
I've created a buffer which reads 4K chunks per read and then the module
parses the data.
If the buffer becomes less then 512B I read another chunk and so on.

Reducing 100K+ reads to around 15K reads should improve the performance.
For some reason it did not happen.
I've played with the chunk size, but nothing came out of it.
Is there a Python specific way to optimise reading from disk?

I'm using Python 2.5.2 with Ubuntu 8.10 32bit

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


Re: tkinter questions: behavior of StringVar, etc

2009-03-30 Thread Eric Brunel
Alan G Isaac wrote:
> I'm a complete newbie to GUI.
> I have a couple questions about tkinter.
>
> 1. Where is the list of changes
> in Python 3's tkinter?

I'll let someone else answer this as I don't use Python 3 myself. I guess
there are not many.

> 2. What exactly is the role of the root object,
> traditionally created as ``root=tk.Tk()``?
> What is an example where one should create this
> before creating a Frame instance (which will
> otherwise implicitly create one as its master)?

The object traditionally called root is in fact an instance of the tcl
interpreter that will get the commands generated by the Tkinter module. Due to
tk architecture, creating this interpreter will also create a window, which is
inteneded to be your application's main window. This window is called '.' in
tcl/tk. The root object in Tkinter then represents this '.' window. So the
instance of Tk is actually 2 things:
- The interpreter itself;
- The main window for your application.

As for when you should create explicitely an instance of Tk, well, I'd say
always ;-) Creating an instance of Frame without creating a Tk instance first
will actually create one, but you'll have no direct access to it. And you
might want an access to it for quite a number of reasons: hiding it, make it
an icon, add menus to it, and so on... All these operations can be done on
actual windows, not on a Frame which is just a container widget.

> 2. Suppose I ``import tkinter as tk`` and
> then try ``s1=tk.StringVar()``.  This fails
> because no "master" is set. Why does a
> Variable need a master?

Because it needs a tcl interpreter to be created. All Tkinter widget actually
reference their interpreter in their tk attribute. The StringVar will probably
just use that.

> 3. Now suppose I set ``root = tk.TK()`` and
> then try ``s1=tk.StringVar()``.  This
> works fine but now seems a bit magical:
> how has the value of the "master" been
> set?

The Tk instance is registered in a hidden variable in the Tkinter module. When
you don't specify a master, it'll use the latest created Tk instance one by
default. BTW, the latest should be the only one: it is quite unsafe to create
several Tk instances in the same application.

> 4. Another bit of magic:
> Suppose I ``import tkinter as tk`` and
> then try ``f1=tk.Frame()``.  This works
> fine: apparently calling Frame also
> leads to implicit creation of a "master".
> Why is what is good for the gander (i.e.,
> implicit master creation for a Frame) not
> good for the goose (i.e., a Variable)?
> (Here I assume that there has been an
> answer to 2. above.)

Well, I personnally don't see any point on doing any master creation
implicitely, so I never use this master auto-creation for anything. I guess
that having a window automatically created when you just try to instantiate a
variable has been considered weird. But it's just a guess...

> 5. Reading around a bit,
> it seems common to recommend setting
> the values of Variables rather than initializing
> them.  Why?  I cannot see the reason to avoid
> ``s1=tk.StringVar(value="this works fine")``
> and it looks like ``tk.StringVar(()`` is in any
> case initialized (to an empty string).

I've never seen such a recommendation anywhere. I do tend to rely on the
variable's default values.

> 6. Why is str(s1) not its value?  More generally,
> why does a StringVar not behave more like a string?

Well, that's a question for the guys who made the Tkinter module. My guess
would be that StringVar's are supposed to be used only to communicate between
the Python layer and the tcl one. They are not intended to be used as actual
strings in your application. Don't forget anything you do on a StringVar is
actually done by tcl, not Python. So I guess there is also a performance
penalty to use StringVar's instead of Python strings.

> Thanks for any insights,
> Alan Isaac

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


Re: dict view to list

2009-03-30 Thread Tim Hoffman
Hi Aaron

I personally don't understand how somedict.values().to_list() is
actually preferable to
list(somedict.keys())

In the standard python idiom I am constructing a new object (which I
can control the type of) using a standard language mechanism (and I
can substitute list with set or for that matter
any other class that accepts an iterable as initial argument)  where
as what you advocate
is calling some special method of an instance which in fact is a
factory for instantiating some
other class, this seems to be less flat than the standard idiom and
significantly less flexible.

and more typing ;-)

How do you see the to_list() to be better or contributing the the user
experience ?

Rgds

Tim Hoffman

On Mar 27, 1:44 pm, Aaron Brady  wrote:
> Hi.
>
> Is there a possibility of the dict_values, dict_items, and dict_keys
> objects growing a 'tolist' method?  It's one of those little things
> that contributes to one's user experience.
>
> P.S.  Yes, yes, I know, -1.

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


database connection error - postgresql

2009-03-30 Thread bijoy
Hi,

*code:* (only the class definiton and Database connection part)

import pg

__metaclass__=type

class addbook:

conn=pg.connect('secondbooks.db')
curs=conn.cursor()

*error:*

conn=pg.connect("secondbooks.db")
pg.InternalError: FATAL:  database "secondbooks.db" does not exist


In fact i have a database called secondbooks in postgresql.

Pls help on this as i am new to python.

Thanks in advance

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


Re: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?)

2009-03-30 Thread Paul Rubin
"Hendrik van Rooyen"  writes:
> Its kind of sad to see unladen swallow, which is just
> a promise, on the list, while Shedskin, which isn't, 
> is ignored.
> 
> Does this say something about big corporations
> vs the small man?

I think the programs on the list were supposed to actually implement
Python and extensions of Python, but not incompatible dialects.
Otherwise Pyrex (for example) would also be on the list.
--
http://mail.python.org/mailman/listinfo/python-list


Re: SQL Query shows error in program while it function correctly in psql interface

2009-03-30 Thread bijoy
Hi,

I have figured this out.

Pls excuse me.

thanks a lot

Bijoy

On Mon, Mar 30, 2009 at 12:41 PM, bijoy  wrote:

> Hi,
>
> Following SQL assaignment is not working in Program.
>
> intable_bookname=SELECT book_name FROM table_book WHERE book_name='name';
>
> where as this works perfectly fine with psql interface
>
> Pls help me on this
>
> Thanks a lot in advance
>
> Bijoy
>
>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ordered Sets

2009-03-30 Thread Nick Craig-Wood
Aahz  wrote:
> I find the trick of using a Python list to store the doubly-linked
> list difficult to understand (as opposed to the usual mechanism of a
> node class).  I understand why it was done (time and space
> efficiency), but I also still feel emotionally that it's somewhat
> sick and perverted.  I probably would feel more comfortable if the
> doubly-linked list were abstracted out and commented.  Heck, the
> whole thing needs unit tests.

Hmmm... Lets compare the two.

Here is the length three list

$ python
Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> L = []
>>> for i in xrange(100):
... L.append([1,2,3])
...
>>> import os
>>> os.getpid()
28134
>>>

(from top)
28134 ncw   20   0 58860  53m 1900 S0  2.6   0:02.62 python

vs a Node class with __slots__ (for efficiency)

$ python
Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Node(object):
... __slots__ = ["prev", "next", "this"]
... def __init__(self, prev, next, this):
... self.prev = prev
... self.next = next
... self.this = this
...
>>> Node(1,2,3)
<__main__.Node object at 0xb7e897cc>
>>> Node(1,2,3).prev
1
>>> L = []
>>> for i in xrange(100):
... L.append(Node(1,2,3))
...
>>> import os
>>> os.getpid()
28203
>>>

(from top)
28203 ncw   20   0 43364  38m 1900 S0  1.9   0:04.41 python

So the Node class actually takes less memory 38 Mbytes vs 53 Mbytes for
the list.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread Hrvoje Niksic
[email protected] writes:

> So I want a class ShadowRegister, which just has a value that I can
> do get/set bit sel and slice ops.  I got that working with __init__.
> It was subclass from "object".  Then I wanted a RegisterClass that
> was a subclass of ShadowRegister, which would read a hardware
> register before doing get bit sel/slices, or read HW reg, do set bit
> sel/slice, but when I try to print in hex format ('0x016X') it said
> it required an int (but the ShadowRegister class had no issues).
> Then I was told instead of using object I could subclass as long
> (seemed the only solution for Python 2.5).  Then when I started to
> want to add my own init code (like register length in bits), I was
> told I should use __new__ instead of __init__.  So but ever since
> then I've noticed that my value is not changing from the initially
> set value.  I'm really cornfused now.

I think I understand your problem.  The short story is: if you derive
from int or long, you won't be able to change the "value" because the
underlying value is immutable.  To get mutable values, you'll need to
subclass object and implement the int-like functionality you need.
Fortunately, this is quite easy.

I assume that by "print in hex format" you're referring to the %
operator, such as '%x' % your_instance.  For it to work, you need to
define an __int__ method (not to be confused with __init__!), which
will get called when coercion to integer is required.  For example:

class Foo(object):
  def __init__(self, initval=0):
self._value = initval
  # a bunch of methods for getting/setting bits
  def __int__(self):
return self._value

>>> x = Foo(100)
>>> '0x%x' % x
'0x64'
>>> x._value = 1000# in real code you'd do this by setting the
   # bits or whatever
>>> '0x%x' % x
'0x3e8'
--
http://mail.python.org/mailman/listinfo/python-list


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread Jan Decaluwe

[email protected] wrote:


Python 2.5.3 for business reasons.

So I want a class ShadowRegister, which just has a value that I can do
get/set bit sel and slice ops.  I got that working with __init__.  It
was subclass from "object".  Then I wanted a RegisterClass that was a
subclass of ShadowRegister, which would read a hardware register
before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
but when I try to print in hex format ('0x016X') it said it required
an int (but the ShadowRegister class had no issues).  Then I was told
instead of using object I could subclass as long (seemed the only
solution for Python 2.5).  Then when I started to want to add my own
init code (like register length in bits), I was told I should use
__new__ instead of __init__.  So but ever since then I've noticed that
my value is not changing from the initially set value.  I'm really
cornfused now.


In the past, someone referred you to the intbv class in MyHDL.
You mentioned that it does "more than you want".
However, it seems to me that what intbv really does, is to solve
the kind of issues that you are struggling with. Perhaps
you want to look at it again.

Jan

--
Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com
   Python as an HDL: http://www.myhdl.org
   VHDL development, the modern way: http://www.sigasi.com
   Analog design automation: http://www.mephisto-da.com
   World-class digital design: http://www.easics.com
--
http://mail.python.org/mailman/listinfo/python-list


Re. suid/sudo in python

2009-03-30 Thread Rustom Mody
Ben Finney wrote
> The key thing to realise is that, having relinquished privilege, the same 
> process can't get it back again as easily. So if you need to
> do some tasks as a privileged user, do those *very* early and then drop the 
> privileges for the rest of the life of the process.
>
> Taking this further, you should isolate exactly what tasks need root 
> privilege into a separate process altogether, and make
> that process as well-tested and simple as possible: it should do nothing 
> *but* those tasks for which it needs root privilege.

I dont think this would be easy or convenient (if at all possible) in my case.

I am trying to write a tiny web based application that will give an
overall picture of LVM, Volume groups, Raid, SCSI and the underlying
disk partitions. The administrative tools dealing with low level
storage stack (e.g. fdisk, pvcreate, vgcreate, lvcreate, mdadm etc.)
need to be run as root.

However since this runs behind apache. Apache creates a separate user
for the webserver. Hence the CGI scripts or any other tools that they
call run as that user.

The solution currently is
- Write the CGI program in C, put setuid(0), setgid(0) statements in
that file and then perform any other actions (including calling other
scripts)
- Set the S bit of the executable of the CGI binary compiled from the
C file (chmod +S xxx.cgi)

Yeah yeah "Security! HOLE!!" etc but please note that this is running
on linux on vmware on an otherwise secure system.

So whats the best way of doing this in python?
--
http://mail.python.org/mailman/listinfo/python-list


Re: smtplib problem with newly rebuilt Debian/lenny system

2009-03-30 Thread BJ Swope
try
 s=smtplib.SMTP('127.0.0.1')

instead.  I'm guessing that it's trying to bind to the IPv6 or some other
non IPv4 localhost instance.

On Wed, Mar 18, 2009 at 11:25 AM, cassiope  wrote:

> A hard drive failure forced me to rebuild my main system.  Just a few
> things haven't been restored; one of them is a python script which is
> used to email users of important events.
>
> In attempting to diagnose the cause, I tried directly executing the
> lines inside the python2.5 interpreter:
>
>import  smtplib
>s= smtplib.SMTP('localhost')
>
> but the second line causes  a traceback:
>
>File "", line 1, in 
>File "/usr/lib/python2.5/smtplib.py", line 244, in __init__
>(code, msg) = self.connect(host, port)
>File "/usr/lib/python2.5/smtplib.py", line 310, in connect
>raise socket.error, msg
>socket.error: (97, 'Address family not supported by protocol')
>
> This is with exim4 and python2.5 on a newly installed lenny system.
> No error messages appear in /var/log or /var/log/exim4 directories.
>
> Helpful clues or pointers to relevant documentation would be
> appreciated!
>
>-f
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
We are all slave to our own paradigm. -- Joshua Williams

If the letters PhD appear after a person's name, that person will remain
outdoors even after it's started raining. -- Jeff Kay

Fascism is a term used to describe authoritarian nationalist political
ideologies or mass movements that are concerned with notions of cultural
decline or decadence and seek to achieve a millenarian national rebirth by
exalting the nation or race, and promoting cults of unity, strength and
purity. - Wikipedia

The story of postwar American conservatism is best understood as a continual
replay of a single long-standing debate. On one side are those who have
upheld the Burkean ideal of replenishing civil society by adjusting to
changing conditions. On the other are those committed to a revanchist
counterrevolution, the restoration of America's pre-welfare state ancien
regime. And, time and again, the counterrevolutionaries have won. The result
is that modern American conservatism has dedicated itself not to fortifying
and replenishing civil society but rather to weakening it through a politics
of civil warfare. -- Sam Tanenhaus
--
http://mail.python.org/mailman/listinfo/python-list


Caught out by daylight saving :-(

2009-03-30 Thread CinnamonDonkey
Hi All,

I had the following bit of code which was working fine until we went
into Daylight saving this weekend, now the result is an hour out.

timeString = "20090330 15:45:23"

timeFormat = '%Y-%m-%d %H:%M:%S'

modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
( time.strptime( timeString, timeFormat ) ) )
minutesToAdvance = datetime.timedelta( minutes=5 )

modificationTime = modificationTime + minutesToAdvance

datetimeString = str ( modificationTime ).replace( ' ', 'T' )


The expected result should be:

datetimeString = "20090330T15:50:23"

But instead I get:

datetimeString = "20090330T14:50:23"

I believe it is going wrong at either the mktime() or utcfromtimestamp
() stage.

What is the correct way to fix this compensating for daylight saving
automatically?

Regards,
SHaun >8)

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


Re: Caught out by daylight saving :-(

2009-03-30 Thread Chris
On Mar 30, 1:47 pm, CinnamonDonkey 
wrote:
> Hi All,
>
> I had the following bit of code which was working fine until we went
> into Daylight saving this weekend, now the result is an hour out.
>
>     timeString = "20090330 15:45:23"
>
>     timeFormat = '%Y-%m-%d %H:%M:%S'
>
>     modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> ( time.strptime( timeString, timeFormat ) ) )
>     minutesToAdvance = datetime.timedelta( minutes=5 )
>
>     modificationTime = modificationTime + minutesToAdvance
>
>     datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
> The expected result should be:
>
>     datetimeString = "20090330T15:50:23"
>
> But instead I get:
>
>     datetimeString = "20090330T14:50:23"
>
> I believe it is going wrong at either the mktime() or utcfromtimestamp
> () stage.
>
> What is the correct way to fix this compensating for daylight saving
> automatically?
>
> Regards,
> SHaun >8)

Take a look at the datetime docs
http://docs.python.org/library/datetime.html#datetime.tzinfo.dst
--
http://mail.python.org/mailman/listinfo/python-list


Re: Caught out by daylight saving :-(

2009-03-30 Thread andrew cooke

these are utilities i use that might help:

def parse_utc_date(day, formats=None):
'''
Return the epoch for a given UTC date.
'''
if day is None:
return time()
if formats is None:
formats = ('%Y-%m-%d %H:%M:%S %Z', '%Y-%m-%d %Z', '%Y-%B-%d %Z')
day = day.strip()
if not day.endswith('UTC'):
day += ' UTC'
exception = None
for format in formats:
try:
return timegm(strptime(day, format))
except Exception, e:
#LOG.debug(format_exc())
exception = e
if exception:
raise ValueError(_('Could not parse %s: %s') % (day, exception))
else:
raise ValueError(_('Could not parse %s') % (day))

def format_utc_date(epoch, format='%Y-%m-%d %H:%M:%S UTC'):
date_ = datetime.utcfromtimestamp(epoch)
return date_.strftime(format)


andrew


CinnamonDonkey wrote:
> Hi All,
>
> I had the following bit of code which was working fine until we went
> into Daylight saving this weekend, now the result is an hour out.
>
> timeString = "20090330 15:45:23"
>
> timeFormat = '%Y-%m-%d %H:%M:%S'
>
> modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> ( time.strptime( timeString, timeFormat ) ) )
> minutesToAdvance = datetime.timedelta( minutes=5 )
>
> modificationTime = modificationTime + minutesToAdvance
>
> datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
>
> The expected result should be:
>
> datetimeString = "20090330T15:50:23"
>
> But instead I get:
>
> datetimeString = "20090330T14:50:23"
>
> I believe it is going wrong at either the mktime() or utcfromtimestamp
> () stage.
>
> What is the correct way to fix this compensating for daylight saving
> automatically?
>
> Regards,
> SHaun >8)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: Re. suid/sudo in python

2009-03-30 Thread Rustom Mody
On Mon, Mar 30, 2009 at 5:17 PM, andrew cooke  wrote:
> Rustom Mody wrote:
>> Ben Finney wrote
>>> The key thing to realise is that, having relinquished privilege, the
>>> same process can't get it back again as easily. So if you need to
>>> do some tasks as a privileged user, do those *very* early and then drop
>>> the privileges for the rest of the life of the process.
>>>
>>> Taking this further, you should isolate exactly what tasks need root
>>> privilege into a separate process altogether, and make
>>> that process as well-tested and simple as possible: it should do nothing
>>> *but* those tasks for which it needs root privilege.
>>
>> I dont think this would be easy or convenient (if at all possible) in my
>> case.
>>
>> I am trying to write a tiny web based application that will give an
>> overall picture of LVM, Volume groups, Raid, SCSI and the underlying
>> disk partitions. The administrative tools dealing with low level
>> storage stack (e.g. fdisk, pvcreate, vgcreate, lvcreate, mdadm etc.)
>> need to be run as root.
>
> i think you should isolate exactly what tasks need root privilege into a
> separate process altogether, and make that process as well-tested and
> simple as possible: it should do nothing *but* those tasks for which it
> needs root privilege.
Yes I guess this is the proper way.
But what I am looking for is not so much a proper way as a simple way
Roughly something in python that is like sudo in shell(??) [Dont know
if this is a good analogy]

You see security is not really at issue here.
Rather grappling with stuff that has been armored for much more
stringent security(like apache).

>
> in this case, for example, that would have absolutely nothing to do with
> interfacing to the web - it would focus only on the particular tasks you
> need to do with the administrative tools (it would also take care, for
> example, to allow only "read-like" commands to be executed, by exposing
> explicit methods for those rather than the unix commands as a whole) (are
> you sure the information you need is not available via reading /proc?)

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


Accessing shell output from HTTP

2009-03-30 Thread Brendon Wickham
Hi there,

I would like users of my web application to be able to download a backup
file of a database (using* *MySQL's *mysqldump* command).

My strategy is to use *zipfile* to create a zip file object (with the *
mysqldump* output as the contents of the zipped file) and then use *
sys.stdout* to send the zip file object to the user as a file for them
download.

The zipping bit and file delivery is all sorted. Getting the output from *
mysqldump* is my problem and I'm not even sure (given that, as I understand
it, it's an asynchronous call to shell via an HTTP thread) it is even
possible.

This is as far as I've got:

import subprocess as sp
p1 = sp.Popen('mysqldump --opt
--user=[username]--password=[password]
[databasename]',stdout=sp.PIPE,shell=True)
backupfile=p1.communicate()[0]

If I type the above into a Python prompt and print *backupfile* I will get
the expected result, but when I'm going through CGI (the script, called from
a web browser, is supposed to capture the output into the variable) nothing
is being returned by communicate().

Is this possible? Or am I barking up the wrong tree?

Cheers,

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


Re: PyFits for Windows?

2009-03-30 Thread W. eWatson

W. eWatson wrote:
It looks like PyFits downloads are for Linux. Isn't there anything 
available for Win (xp)?
I'm now on the scipy mail list. Things look hopeful, according to the first 
respondent, to meet my criteria mentioned in another sub-thread to this one:

"I'm hoping the use of this library will be relative simple for my
purposes, which are basically to write an image to a fits file with a
somewhat simple header, which might include lat/long, date, image size,
date-time, and a comment."

Apparently, the first chapter or several pages or so of a manual distributed 
with PyFits is enough.


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: Re. suid/sudo in python

2009-03-30 Thread andrew cooke
Rustom Mody wrote:
> Ben Finney wrote
>> The key thing to realise is that, having relinquished privilege, the
>> same process can't get it back again as easily. So if you need to
>> do some tasks as a privileged user, do those *very* early and then drop
>> the privileges for the rest of the life of the process.
>>
>> Taking this further, you should isolate exactly what tasks need root
>> privilege into a separate process altogether, and make
>> that process as well-tested and simple as possible: it should do nothing
>> *but* those tasks for which it needs root privilege.
>
> I dont think this would be easy or convenient (if at all possible) in my
> case.
>
> I am trying to write a tiny web based application that will give an
> overall picture of LVM, Volume groups, Raid, SCSI and the underlying
> disk partitions. The administrative tools dealing with low level
> storage stack (e.g. fdisk, pvcreate, vgcreate, lvcreate, mdadm etc.)
> need to be run as root.

i think you should isolate exactly what tasks need root privilege into a
separate process altogether, and make that process as well-tested and
simple as possible: it should do nothing *but* those tasks for which it
needs root privilege.

in this case, for example, that would have absolutely nothing to do with
interfacing to the web - it would focus only on the particular tasks you
need to do with the administrative tools (it would also take care, for
example, to allow only "read-like" commands to be executed, by exposing
explicit methods for those rather than the unix commands as a whole) (are
you sure the information you need is not available via reading /proc?)

andrew

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


CPython and C++ object GC

2009-03-30 Thread csaba . balazs
Hello Everybody,

I would like to use a C++ gui library with the following (simplified)
interface in Python.

#include 

class Gui;

class GuiObject {
public:
GuiObject(Gui *Gui) {printf("creating GuiObject(gui: %X)\n", Gui);}
~GuiObject() {printf("deleting GuiObject\n");}
void Move(int x, int y) {printf("GuiObject move(%d, %d)\n", x, y);};
};

class Gui {
public:
Gui()  {printf("creating Gui\n");}
~Gui() {printf("deleting Gui\n");}
GuiObject* AddImage() {
GuiObject* ob = new GuiObject(this);
return ob;
}
void Print() {printf("Gui: %X\n", this);}
};

int main() {
Gui *gui = new Gui();
gui->Print();
GuiObject *obj = gui->AddImage();
obj->Move(50, 50);
/*GuiObject *obj2 = new GuiObject(gui); // not allowed
delete obj2;*/
delete obj;
delete gui;
return 0;
}


I created the Python Gui and GuiObject classes (PyTypeObject), and
added it to main module (PyModule_AddObject).
It works, but there is a problem at the Gui::AddImage(), with
constructs a new GuiObject, which is available in Python layer but
finally it is not collected and freed by GC:

...
obj = _PyObject_New(&GuiObjectType);
PyObject_Init(obj, &GuiObjectType);
...

I cannot invoke the GuiObject object constructor directly from Python,
because of the implementation of the C++ gui library (in this case it
would be collected).
I use the embedded CPython as an interpreter, so I cannot add
additional external .py file for it.

So the following Python code would be the target:

gui = GUI();

background = gui.AddImage();
#background = GuiObject(gui); <-- Collected but not allowed
background.ImageFile("bg.jpg");
background.Move(0, 0);
...

How could I implement the AddImage function in order to be freed the
constructed object at the end?

Thanks in advance!

All-the-best,

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


Re: tkinter questions: behavior of StringVar, etc

2009-03-30 Thread Alan G Isaac

On 3/30/2009 3:37 AM Eric Brunel apparently wrote:

The object traditionally called root is in fact an instance of the tcl
interpreter that will get the commands generated by the Tkinter module. Due to
tk architecture, creating this interpreter will also create a window, which is
inteneded to be your application's main window. This window is called '.' in
tcl/tk. The root object in Tkinter then represents this '.' window. So the
instance of Tk is actually 2 things:
- The interpreter itself;
- The main window for your application.


OK.


As for when you should create explicitely an instance of Tk, well, I'd say
always ;-) Creating an instance of Frame without creating a Tk instance first
will actually create one, but you'll have no direct access to it.


If I start by creating a frame `f`, then ``f.master`` is the root.
Still, I take your point.


And you
might want an access to it for quite a number of reasons: hiding it, make it
an icon, add menus to it, and so on... All these operations can be done on
actual windows, not on a Frame which is just a container widget.


Useful.  Thanks.


All Tkinter widget actually
reference their interpreter in their tk attribute. The StringVar will probably
just use that.


Yes, I see how this works now.


The Tk instance is registered in a hidden variable in the Tkinter module. When
you don't specify a master, it'll use the latest created Tk instance one by
default. BTW, the latest should be the only one: it is quite unsafe to create
several Tk instances in the same application.


I have no desire to do this, but might you pin down "unsafe"?


I guess
that having a window automatically created when you just try to instantiate a
variable has been considered weird. But it's just a guess.


Yes, I am making that same guess.

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


Re: how to optimize zipimport

2009-03-30 Thread Coonay
On Mar 26, 1:38 pm, John Machin  wrote:
> On Mar 26, 2:06 pm, Coonay  wrote:
>
> > see,the zipimort takes nearly 0.5 second
>
> > D 03-25 07:53PM 21.374
> > Loading __main__
> > I 03-25 07:53PM 21.455
> > zipimporter('/base/data/home/apps/coonay/1.332322118600950324/
> > django.zip', '')
>
> ?? zipimport.Zipimporter() has one arg, not two.
>
> > W 03-25 07:53PM 22.046
> > appengine_django module
>
> > On Mar 26, 10:41 am, Coonay  wrote:
>
> > > in my mudule ,i import another mudule from a zip ,
>
> > > when i call  my mudle method,the module in the zip will be import from
> > > the zip every time,that slow down the operation,
>
> > > i try to search the some workarodnd or solution but i don't get one,
>
i don't use zipimport directly,just like u said i put the zip in the
sys.path


> Why are you explicitly using the zipimport module? In other words, why
> don't you put the full path of the zip file in sys.path and then just
> use the normal "import module_in_the_zip" mechanism? Note that this

i found the the module in the zip reloaded  everytime the code is
called,i mean, say ,first time it take me 1 second to call a method in
the zip,but it take another 1 second to call the mothod for another
time ,and so on


> can/should be done once at the top of your calling module. In other
> words, the import code is executed when the calling module is imported
> by your main script, not every time you call a method in your calling
> module.
>
In my developing environment ,there is a limitation of file number
that i can deploy,such as i 'm only allowed to deploy 100 python
files,so i have to zip some module


> Alternatively, if you feel you *must* use zipimport, what's to stop
> you doing that once at the top of the calling module?
>
> Note: it may help when you reply to show a stripped-down version of
> your calling module, sufficient to back up your description of what is
> happening.
>
> HTH,
> John

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


Re: Caught out by daylight saving :-(

2009-03-30 Thread CinnamonDonkey
Hi Chris,

Thanx for the link... I had already found that. My problem is not
finding information but more understanding it. I've only been
Pythoning for a short while and I don't fully understand what the
documentation is getting at sometimes.

Is it saying that I should define a new class inheriting from tzinfo
and refine the behaviour of the dst() function? If so, then what do I
do with the class?



On 30 Mar, 13:08, Chris  wrote:
> On Mar 30, 1:47 pm, CinnamonDonkey 
> wrote:
>
>
>
> > Hi All,
>
> > I had the following bit of code which was working fine until we went
> > into Daylight saving this weekend, now the result is an hour out.
>
> >     timeString = "20090330 15:45:23"
>
> >     timeFormat = '%Y-%m-%d %H:%M:%S'
>
> >     modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> > ( time.strptime( timeString, timeFormat ) ) )
> >     minutesToAdvance = datetime.timedelta( minutes=5 )
>
> >     modificationTime = modificationTime + minutesToAdvance
>
> >     datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
> > The expected result should be:
>
> >     datetimeString = "20090330T15:50:23"
>
> > But instead I get:
>
> >     datetimeString = "20090330T14:50:23"
>
> > I believe it is going wrong at either the mktime() or utcfromtimestamp
> > () stage.
>
> > What is the correct way to fix this compensating for daylight saving
> > automatically?
>
> > Regards,
> > SHaun >8)
>
> Take a look at the datetime 
> docshttp://docs.python.org/library/datetime.html#datetime.tzinfo.dst

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


Re: Programming Python 4th Edition?

2009-03-30 Thread Esmail

Nick Craig-Wood wrote:


I read Programming Python as an experienced programmer and like you I
enjoyed the encyclopedic nature of it.  So if it appeals to you I'd
say go for it!

The fact that it doesn't use the latest version of python isn't a
problem - python doesn't change very quickly and emphasises backwards
compatibility, even for the jump to 3.x.


Cool .. glad to hear that .. esp since all my other Python books are
even a bit more outdated :-)  .. the only thing that I notice missing
is coverage of the subprocess module which I will be using a fair bit
I think.

I had a 40% off coupon for our local Borders, so I couldn't pass that
up to get myself a copy of this book. No regrets.

Cheers,
Esmail


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


Re: Re. suid/sudo in python

2009-03-30 Thread Rustom Mody
The outline of what I do (in C) is:
1. Write the CGI program in C, put setuid(0), setgid(0) statements in
that file and then perform any other actions (including calling other
scripts)
2. Set the S bit of the executable of the CGI binary compiled from the
C file (chmod +S xxx.cgi)

The C code runs thats compiled to xxx.cgi is roughly this:
main(argc, argv)
{
 setuid(0);
 setgid(0);
 /* IO plumbing for get/post */
  execv("/usr/lib/cgi-bin/main.sh",argv);
}
where main.sh does all the root-ish things in shell.

Now I'll be honest here.  This is a friend's code.  He's written it in
C + shell. I said an all-python solution may be easier.  He said "Show
me!"
Now I gather ( 
http://mail.python.org/pipermail/python-list/2000-July/044690.html
)
this is doable in perl and straightforward in C but not possible in python
--
http://mail.python.org/mailman/listinfo/python-list


Re: Caught out by daylight saving :-(

2009-03-30 Thread Dave Angel
There isn't any right answer.  There are two very different ways you can 
interpret daylight savings time on a time conversion.  I suspect you're 
on Windows, trying to look at an old file's datestamp, and expect it to 
look like Windows displays it.  But Windows isn't even consistent with 
itself, doing something different on a FAT system than on NTFS.


For most of my own purposes, I've decided to only store the UTC times 
for things.  Local time is useful only for interactive display.  And 
that interactive display is calculated according to some context.


To illustrate the problem, suppose you were in Chicago last month, and 
modified a file at 2:00 pm, CST.   And now you are located in PDT time 
zone, and want to know when the file was last modified.  Should you 
convert the time zone and daylight savings, or should you convert only 
time zone, or should you display the time as it was known to you at the 
original change?


And to make it more complex, suppose the disk drive involved was located 
in France.  Just what time is correct?


Anything other than UTC is subject to confusion.

CinnamonDonkey wrote:

Hi All,

I had the following bit of code which was working fine until we went
into Daylight saving this weekend, now the result is an hour out.

    timeString = "20090330 15:45:23"

timeFormat = '%Y-%m-%d %H:%M:%S'

modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
( time.strptime( timeString, timeFormat ) ) )
minutesToAdvance = datetime.timedelta( minutes=5 )

modificationTime = modificationTime + minutesToAdvance

datetimeString = str ( modificationTime ).replace( ' ', 'T' )


The expected result should be:

datetimeString = "20090330T15:50:23"

But instead I get:

datetimeString = "20090330T14:50:23"

I believe it is going wrong at either the mktime() or utcfromtimestamp
() stage.

What is the correct way to fix this compensating for daylight saving
automatically?

Regards,
SHaun >8)


  

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


Re: Caught out by daylight saving :-(

2009-03-30 Thread CinnamonDonkey
Ah, I think I needed to use fromtimestamp() and not utcfromtimestamp
().

:-)


On 30 Mar, 13:42, CinnamonDonkey 
wrote:
> Hi Chris,
>
> Thanx for the link... I had already found that. My problem is not
> finding information but more understanding it. I've only been
> Pythoning for a short while and I don't fully understand what the
> documentation is getting at sometimes.
>
> Is it saying that I should define a new class inheriting from tzinfo
> and refine the behaviour of the dst() function? If so, then what do I
> do with the class?
>
> On 30 Mar, 13:08, Chris  wrote:
>
> > On Mar 30, 1:47 pm, CinnamonDonkey 
> > wrote:
>
> > > Hi All,
>
> > > I had the following bit of code which was working fine until we went
> > > into Daylight saving this weekend, now the result is an hour out.
>
> > >     timeString = "20090330 15:45:23"
>
> > >     timeFormat = '%Y-%m-%d %H:%M:%S'
>
> > >     modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> > > ( time.strptime( timeString, timeFormat ) ) )
> > >     minutesToAdvance = datetime.timedelta( minutes=5 )
>
> > >     modificationTime = modificationTime + minutesToAdvance
>
> > >     datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
> > > The expected result should be:
>
> > >     datetimeString = "20090330T15:50:23"
>
> > > But instead I get:
>
> > >     datetimeString = "20090330T14:50:23"
>
> > > I believe it is going wrong at either the mktime() or utcfromtimestamp
> > > () stage.
>
> > > What is the correct way to fix this compensating for daylight saving
> > > automatically?
>
> > > Regards,
> > > SHaun >8)
>
> > Take a look at the datetime 
> > docshttp://docs.python.org/library/datetime.html#datetime.tzinfo.dst
>
>

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


Re: tkinter questions: behavior of StringVar, etc

2009-03-30 Thread Alan G Isaac

On 3/30/2009 3:37 AM Eric Brunel apparently wrote:

The string representation of Tkinter objects seems to be a design principle in
this module: it'll always evaluate to the representation this object has at
tcl level. Since a XxxVar is represented by an actual variable at tcl level,
its string representation is the name of this variable. I guess it's quite
easier to build the commands that'll be passed to the tcl interpreter this
way: you don't have to check the type of the objects you handle, but pass them
through str and insert the result directly in the command.



This is a helpful answer: it "feels" right and can be
explored further.

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


running pyhton IDLE on windows vista

2009-03-30 Thread ryan
Hi guys ,

I am facing  problems running python25 on vista . i was able to
successfully install it but when i try to run it then,
its throws errors saying Firewall issues ..

I tried disabling the firewall but no go..

Thanks in advance!!

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


Re: database connection error - postgresql

2009-03-30 Thread bijoy
hi,

I figured out these too.
Pls excuse me

Thanks

Bijoy


On Mon, Mar 30, 2009 at 1:30 PM, bijoy  wrote:

> Hi,
>
> *code:* (only the class definiton and Database connection part)
>
> import pg
>
> __metaclass__=type
>
> class addbook:
>
> conn=pg.connect('secondbooks.db')
> curs=conn.cursor()
>
> *error:*
>
> conn=pg.connect("secondbooks.db")
> pg.InternalError: FATAL:  database "secondbooks.db" does not exist
>
>
> 
> In fact i have a database called secondbooks in postgresql.
>
> Pls help on this as i am new to python.
>
> Thanks in advance
>
> Bijoy
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Caught out by daylight saving :-(

2009-03-30 Thread CinnamonDonkey
It's not that fancy, but yes I am on windows.

It's a script being called by Cruise Control .NET. They pass in a time
stamp on the command line, "MMDD HH:MM:SS" and I need to advance
it by 5 minutes before writing it back out to STDOUT to fake a source
control modification.

The process stopped working because it was always returning a time
stamp -1 hr due to daylight saving.

Changing utcfromtimestamp() to fromtimestamp() seems to have fixed
it.



On 30 Mar, 13:56, Dave Angel  wrote:
> There isn't any right answer.  There are two very different ways you can
> interpret daylight savings time on a time conversion.  I suspect you're
> on Windows, trying to look at an old file's datestamp, and expect it to
> look like Windows displays it.  But Windows isn't even consistent with
> itself, doing something different on a FAT system than on NTFS.
>
> For most of my own purposes, I've decided to only store the UTC times
> for things.  Local time is useful only for interactive display.  And
> that interactive display is calculated according to some context.
>
> To illustrate the problem, suppose you were in Chicago last month, and
> modified a file at 2:00 pm, CST.   And now you are located in PDT time
> zone, and want to know when the file was last modified.  Should you
> convert the time zone and daylight savings, or should you convert only
> time zone, or should you display the time as it was known to you at the
> original change?
>
> And to make it more complex, suppose the disk drive involved was located
> in France.  Just what time is correct?
>
> Anything other than UTC is subject to confusion.
>
> CinnamonDonkey wrote:
> > Hi All,
>
> > I had the following bit of code which was working fine until we went
> > into Daylight saving this weekend, now the result is an hour out.
>
> >     timeString = "20090330 15:45:23"
>
> >     timeFormat = '%Y-%m-%d %H:%M:%S'
>
> >     modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> > ( time.strptime( timeString, timeFormat ) ) )
> >     minutesToAdvance = datetime.timedelta( minutes=5 )
>
> >     modificationTime = modificationTime + minutesToAdvance
>
> >     datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
> > The expected result should be:
>
> >     datetimeString = "20090330T15:50:23"
>
> > But instead I get:
>
> >     datetimeString = "20090330T14:50:23"
>
> > I believe it is going wrong at either the mktime() or utcfromtimestamp
> > () stage.
>
> > What is the correct way to fix this compensating for daylight saving
> > automatically?
>
> > Regards,
> > SHaun >8)
>
>

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


Re: Read Garmin XML (TCX) files in Python...

2009-03-30 Thread cmalmqui
Elementtree solved the problem! I am very impressed with the speed you
get from the cElementTree library. Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Wing IDE Backup configuration settings?

2009-03-30 Thread Raúl Gómez C .
Find the .wingide folder in your home and tar it, at least in linux...

On Sat, Mar 28, 2009 at 12:26 PM, John Doe  wrote:

>
> Anyone know how to back up the configuration settings like font sizes
> and colors in the Wing IDE? Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Nacho
Linux Counter #156439
--
http://mail.python.org/mailman/listinfo/python-list


Re: Improve module performance by reducing disk reads

2009-03-30 Thread Dave Angel
Since the email contains no code, I can only assume you're using the 
bult-in open() call, and file.read().   If you didn't specify a bufsize, 
a "system default" is used.  I seriously doubt if 0 is the default.


Since it's already buffered, additional buffering by you might have 
little effect, or even negative effect.  I'd suggest explicitly 
specifying a buffer size in the open() call, starting with 4096, as a 
good starting place.  Then I'd do benchmarks with larger and smaller 
values, to see what differences it might make.


The only time additional buffering tends to be useful is if you know the 
file usage pattern, and it's predictable and not sequential.  Even then, 
it's good to know the underlying buffer's behavior, so that your 
optimizations are not at cross purposes.


I'd expect your performance problems are elsewhere.

kian tern wrote:

Hi all,

I'm writing in Python for about 2 weeks (moved from Perl)
I've ported one of my modules which is a parser for a binary format (see
link bellow for the format specs)

http://etidweb.tamu.edu/cdrom0/image/stdf/spec.pdf

In the first version of the parser I was reading exactly the amount of data
I need to parse
For example 4 bytes per each header
The STDF files tend to be about 40+ MB size with 100K+ records, so I had at
least 1 disk read per record, sometimes 2.

Obviously it's not an efficient way to do it.
I've created a buffer which reads 4K chunks per read and then the module
parses the data.
If the buffer becomes less then 512B I read another chunk and so on.

Reducing 100K+ reads to around 15K reads should improve the performance.
For some reason it did not happen.
I've played with the chunk size, but nothing came out of it.
Is there a Python specific way to optimise reading from disk?

I'm using Python 2.5.2 with Ubuntu 8.10 32bit

Thanks in advance.

  

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


Re: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?)

2009-03-30 Thread srepmub

> > Its kind of sad to see unladen swallow, which is just
> > a promise, on the list, whileShedskin, which isn't,
> > is ignored.
>
> > Does this say something about big corporations
> > vs the small man?
>
> I think the programs on the list were supposed to actually implement
> Python and extensions of Python, but not incompatible dialects.
> Otherwise Pyrex (for example) would also be on the list.

for the record, the input for Shedskin is pure Python, so there is no
added syntax or optional type declaration system. that said, I can
understand it not being on some list for not being production-ready.


thanks,
mark dufour.
--
http://mail.python.org/mailman/listinfo/python-list


Thoughts on language-level configuration support?

2009-03-30 Thread jfager
I've written a short post on including support for configuration down
at the language level, including a small preliminary half-functional
example of what this might look like in Python, available at
http://jasonfager.com/?p=440.

The basic idea is that a language could offer syntactic support for
declaring configurable points in the program.  The language system
would then offer an api to allow the end user to discover a programs
configuration service, as well as a general api for providing
configuration values.

The included example implements the first bit and hints at the third,
defining a function that looks up what variable its output will be
assigned to and tries to find a corresponding value from a
configuration source.  It's very preliminary, but I hope it gives a
flavor of the general idea.

Any thoughts or feedback would be greatly appreciated.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re. suid/sudo in python

2009-03-30 Thread bieffe62
On Mar 30, 1:16 pm, Rustom Mody  wrote:
> Ben Finney wrote
>
> > The key thing to realise is that, having relinquished privilege, the same 
> > process can't get it back again as easily. So if you need to
> > do some tasks as a privileged user, do those *very* early and then drop the 
> > privileges for the rest of the life of the process.
>
> > Taking this further, you should isolate exactly what tasks need root 
> > privilege into a separate process altogether, and make
> > that process as well-tested and simple as possible: it should do nothing 
> > *but* those tasks for which it needs root privilege.
>
> I dont think this would be easy or convenient (if at all possible) in my case.
>
> I am trying to write a tiny web based application that will give an
> overall picture of LVM, Volume groups, Raid, SCSI and the underlying
> disk partitions. The administrative tools dealing with low level
> storage stack (e.g. fdisk, pvcreate, vgcreate, lvcreate, mdadm etc.)
> need to be run as root.
>
> However since this runs behind apache. Apache creates a separate user
> for the webserver. Hence the CGI scripts or any other tools that they
> call run as that user.
>
> The solution currently is
> - Write the CGI program in C, put setuid(0), setgid(0) statements in
> that file and then perform any other actions (including calling other
> scripts)
> - Set the S bit of the executable of the CGI binary compiled from the
> C file (chmod +S xxx.cgi)
>
> Yeah yeah "Security! HOLE!!" etc but please note that this is running
> on linux on vmware on an otherwise secure system.
>
> So whats the best way of doing this in python?

Have a 'server process' running with root privilege ( a script started
by a privileged account)  and implement a protocol to ask for system
info from your cgi scripts under apache. In python this is a lot
easier than it sounds.
The simplest case would be that to send a 'system command' to the
server through a unix socket, the server
executes the command as received and returns the command output. Not
more than a day work, I believe. Not much more secure that
a setuid python script, also, maybe less :-)
A better implementation would be such that the protocol only allows
for a set of pre-defined safe requests ...

Ciao
--
FB

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


Re: database connection error - postgresql

2009-03-30 Thread D'Arcy J.M. Cain
On Mon, 30 Mar 2009 13:30:18 +0530
bijoy  wrote:
> conn=pg.connect("secondbooks.db")
> pg.InternalError: FATAL:  database "secondbooks.db" does not exist

> In fact i have a database called secondbooks in postgresql.

If it is called "secondbooks" then why are you connecting to
"secondbooks.db" in the code?

-- 
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: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
On Mar 30, 2:30 am, Jan Decaluwe  wrote:
> [email protected] wrote:
> > Python 2.5.3 for business reasons.
>
> > So I want a class ShadowRegister, which just has a value that I can do
> > get/set bit sel and slice ops.  I got that working with __init__.  It
> > was subclass from "object".  Then I wanted a RegisterClass that was a
> > subclass of ShadowRegister, which would read a hardware register
> > before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
> > but when I try to print in hex format ('0x016X') it said it required
> > an int (but the ShadowRegister class had no issues).  Then I was told
> > instead of using object I could subclass as long (seemed the only
> > solution for Python 2.5).  Then when I started to want to add my own
> > init code (like register length in bits), I was told I should use
> > __new__ instead of __init__.  So but ever since then I've noticed that
> > my value is not changing from the initially set value.  I'm really
> > cornfused now.
>
> In the past, someone referred you to the intbv class in MyHDL.
> You mentioned that it does "more than you want".
> However, it seems to me that what intbv really does, is to solve
> the kind of issues that you are struggling with. Perhaps
> you want to look at it again.
>
> Jan
>
> --
> Jan Decaluwe - Resources bvba -http://www.jandecaluwe.com
>     Python as an HDL:http://www.myhdl.org
>     VHDL development, the modern way:http://www.sigasi.com
>     Analog design automation:http://www.mephisto-da.com
>     World-class digital design:http://www.easics.com- Hide quoted text -
>
> - Show quoted text -

Hi Hrvoje, Jan;

This is about what I actually originally had, it was derived from
"object", like the intbv class from MyHDL.  And so my ShadowRegister
class was modeled similar to that.  It worked grea.  Then ChipRegister
was made to be a subclass of ShadowRegister.  It needs to read HW reg
and adjust ChipRegister value, write HW reg and adjust ShadowRegister
value.  As mentioned, that's when printing in hex gave an error said
it needed an int.  My story from that point continues from my last
past.  :)

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


Re: Caught out by daylight saving :-(

2009-03-30 Thread Dave Angel
That should work except for the few minutes before or after the 
daylight-savings-time change.  In other words between 1:55 and 2:01am on 
that particular date, twice a year.


During that time you still have to decide what you want to have happen, 
and then test your program to make sure it matches your expectations.  
Remember there's an hour in the fall whose corrected time is ambiguous, 
and there's a representation in the spring that is invalid (there are no 
valid times between 2 and 3 on that day).  Or you can just punt, and 
decide nobody's going to care during those times.


CinnamonDonkey wrote:

It's not that fancy, but yes I am on windows.

It's a script being called by Cruise Control .NET. They pass in a time
stamp on the command line, "MMDD HH:MM:SS" and I need to advance
it by 5 minutes before writing it back out to STDOUT to fake a source
control modification.

The process stopped working because it was always returning a time
stamp -1 hr due to daylight saving.

Changing utcfromtimestamp() to fromtimestamp() seems to have fixed
it.



On 30 Mar, 13:56, Dave Angel  wrote:
  

There isn't any right answer.  There are two very different ways you can
interpret daylight savings time on a time conversion.  I suspect you're
on Windows, trying to look at an old file's datestamp, and expect it to
look like Windows displays it.  But Windows isn't even consistent with
itself, doing something different on a FAT system than on NTFS.

For most of my own purposes, I've decided to only store the UTC times
for things.  Local time is useful only for interactive display.  And
that interactive display is calculated according to some context.

To illustrate the problem, suppose you were in Chicago last month, and
modified a file at 2:00 pm, CST.   And now you are located in PDT time
zone, and want to know when the file was last modified.  Should you
convert the time zone and daylight savings, or should you convert only
time zone, or should you display the time as it was known to you at the
original change?

And to make it more complex, suppose the disk drive involved was located
in France.  Just what time is correct?

Anything other than UTC is subject to confusion.

CinnamonDonkey wrote:


Hi All,
  
I had the following bit of code which was working fine until we went

into Daylight saving this weekend, now the result is an hour out.
  
timeString =20090330 15:45:23"
  
timeFormat =%Y-%m-%d %H:%M:%S'
  
modificationTime =atetime.datetime.utcfromtimestamp( time.mktime

( time.strptime( timeString, timeFormat ) ) )
minutesToAdvance =atetime.timedelta( minutes=5 )
  
modificationTime =odificationTime + minutesToAdvance
  
datetimeString =tr ( modificationTime ).replace( ' ', 'T' )
  
The expected result should be:
  
datetimeString =20090330T15:50:23"
  
But instead I get:
  
datetimeString =20090330T14:50:23"
  
I believe it is going wrong at either the mktime() or utcfromtimestamp

() stage.
  
What is the correct way to fix this compensating for daylight saving

automatically?
  
Regards,

SHaun >8)
  




  

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe

It needs to read HW reg and adjust ChipRegister value, or
ShadowRegister and change the HW value (order was reversed).  I will
look at MyHDL again though to see if there is some reason it can print
class subclassed from intbv, or if it even has a class subclassed from
intbv, without casting as a long in the main program.

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


Re: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?)

2009-03-30 Thread Michele Simionato
On Mar 30, 3:31 pm, srepmub  wrote:
> for the record, the input for Shedskin is pure Python, so there is no
> added syntax or optional type declaration system. that said, I can
> understand it not being on some list for not being production-ready.
>
> thanks,
> mark dufour.

But does ShedSkin accepts all valid Python constructs?
I thought there were restrictions.
--
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter questions: behavior of StringVar, etc

2009-03-30 Thread Eric Brunel
Alan G Isaac wrote:
[snip]
> On 3/30/2009 3:37 AM Eric Brunel apparently wrote:
>> The Tk instance is registered in a hidden variable in the Tkinter module.
When
>> you don't specify a master, it'll use the latest created Tk instance one by
>> default. BTW, the latest should be the only one: it is quite unsafe to
create
>> several Tk instances in the same application.
>
> I have no desire to do this, but might you pin down "unsafe"?

It is not supposed to be unsafe at tcl level, as it supports several
interpreters side by side in the same application. It is unsafe at user level,
precisely because creating a StringVar might put it in the interpreter you
don't want, leading to weird errors and unexpected results.

So you *can* use several Tk instances in the same application, but then you
really should specify a master for absolutely everything, or you'll get bitten
at some point.

> Thanks!
> Alan Isaac

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


Deleteing empty directories

2009-03-30 Thread CinnamonDonkey
Hi All,

I've been scratching my head all afternoon trying to work out the best/
quickest way is to delete empty directories within a tree (Windows).

I've looked at os.walk() but it seems to traverse the directory tree
in the wrong order (is it possible to reverse the order?)

It seems the only way is to manually walk the tree myself recursively
and then back up deleteing a directory if it is found to be empty.

Any ideas?

Cheers,
SHaun >8)

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


Re: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?)

2009-03-30 Thread Steven D'Aprano
On Mon, 30 Mar 2009 09:46:40 +0200, Hendrik van Rooyen wrote:

> Its kind of sad to see unladen swallow, which is just a promise, on the
> list, while Shedskin, which isn't, is ignored.
> 
> Does this say something about big corporations vs the small man?

No, what it says is that I had just read a post about Unladen Swallow two 
minutes before, and I forgot completely about Shedskin.

Although I took a few minutes to google, I knew I'd probably missed 
something, which is why I said there are *at least* thirteen 
implementations of Python.


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


Re: complaints about no replies last week

2009-03-30 Thread pruebauno
On Mar 28, 11:07 am, Aaron Brady  wrote:
> Hi,
>
> A week ago, I posted a question and an idea about Python's garbage
> collector.  I got a few replies.  Some days later, I posted a mock-up
> implementation of it, and got *NO* replies.  Does this mean:
>
> a) It works
> b) It doesn't work
> c) It's not particularly applicable to Python at that point
> (particularly)
> d) It's not news
>
> Thanks and sincerely.
>
> P.S.  Non-plugging 
> links:http://groups.google.com/group/comp.lang.python/browse_thread/thread/...http://groups.google.com/group/comp.lang.python/browse_thread/thread/...

e) It is a hard or complex problem that requires significant
investment of time on a problem or approach that few people are
interested in at the moment.

f) The description is confusing or incomplete or the source code is
long and difficult to read.

I myself asked about how to write a library to efficiently do union
and intersection of sets containing time intervals some time ago on
this list and got little to no answers. It is a tricky problem. Since
I was getting paid I got an O(n*n) solution working. People on this
list on the other hand do not get paid and answer whatever strikes
their fancy. Sometimes the question is hard or confusing and nobody is
motivated enough to answer.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Deleteing empty directories

2009-03-30 Thread Tim Golden

CinnamonDonkey wrote:

Hi All,

I've been scratching my head all afternoon trying to work out the best/
quickest way is to delete empty directories within a tree (Windows).

I've looked at os.walk() but it seems to traverse the directory tree
in the wrong order (is it possible to reverse the order?)

It seems the only way is to manually walk the tree myself recursively
and then back up deleteing a directory if it is found to be empty.


In general, the place to look for these things in the
stdlib is usually shutil. (Slightly awkward that
"shell" in Windows means everything that happens on
the desktop, while "shell" in Unix means everything
*except* what happens on the desktop! This is the
Unix meaning.)

And sure enough...

"""
rmtree( path[, ignore_errors[, onerror]]) 

Delete an entire directory tree (path must point to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception. 
If onerror is provided, it must be a callable that accepts three parameters: function, path, and excinfo. The first parameter, function, is the function which raised the exception; it will be os.listdir(), os.remove() or os.rmdir(). The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information return by sys.exc_info(). Exceptions raised by onerror will not be caught. 


"""


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


Re: c.l.py dead, news at 11 (was Re: Mangle function name with decorator?)

2009-03-30 Thread Tim Golden

Steven D'Aprano wrote:

On Mon, 30 Mar 2009 09:46:40 +0200, Hendrik van Rooyen wrote:


Its kind of sad to see unladen swallow, which is just a promise, on the
list, while Shedskin, which isn't, is ignored.

Does this say something about big corporations vs the small man?


No, what it says is that I had just read a post about Unladen Swallow two 
minutes before, and I forgot completely about Shedskin.


Although I took a few minutes to google, I knew I'd probably missed 
something, which is why I said there are *at least* thirteen 
implementations of Python.


I was going to post in your defence (before realising that
you were more than capable of doing that for yourself).
It was obviously just a casual list, not the official
definitive everything-not-here-is-not-valid Python-lookalikes
competition entrants list. :)

But I was impressed you managed to get so many: I was
aware of everything on that list, but I'm quite sure I
couldn't have named them all together.

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


urllib2, proxies, and pac files on OS X

2009-03-30 Thread Mani Ghasemlou
Hi all,

urllib2 correctly detects proxies as configured in my preferences pane
on OS X 10.5:

Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> urllib2.ProxyHandler().proxies
{'http': 'http://openproxy.in.tungle.com:8080'}
>>>

However, when configuring a proxy via PAC file, this does not seem to
be the case:

Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> urllib2.ProxyHandler().proxies
{}
>>>

Is there any way to obtain (and parse using something like pacparser)
the PAC file via urllib2 (or some other standard python library), or
is this something I need to tackle using Cocoa (in my case PyObjC)?

Thanks a lot!

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


create a log level for python logging module

2009-03-30 Thread dj
I am trying to create a log level called userinfo for the python
logging. I read the source code and tried to register the level to the
logging namespace with the following source:

 from logging import Logger

 # create the custom log level
  class userinfo(Logger):
   def userinfo(self, msg,
*args, **kwargs):
  if
self.isEnabledFor(WARNING):
 
self._log(WARNING, msg, args, **kwargs)

  # Register log level in the
logging.Logger namespace
  Logger.userinfo = userinfo


Has I am sure you guessed, it did not work. If you know how this is
done or know what I am doing work or can provide a link to example
code (because I have not been able to locate any), I would greatly
appreciate it.
My sincere and heartfelt thanks in advance.


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


Re: Deleteing empty directories

2009-03-30 Thread andrew cooke
CinnamonDonkey wrote:
> Hi All,
>
> I've been scratching my head all afternoon trying to work out the best/
> quickest way is to delete empty directories within a tree (Windows).
>
> I've looked at os.walk() but it seems to traverse the directory tree
> in the wrong order (is it possible to reverse the order?)

the documentation for os.walk() explains how to reverse the order.

http://docs.python.org/3.0/library/os.html#os.walk

andrew


>
> It seems the only way is to manually walk the tree myself recursively
> and then back up deleteing a directory if it is found to be empty.
>
> Any ideas?
>
> Cheers,
> SHaun >8)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: Unit testing frameworks

2009-03-30 Thread Alexander Draeger

Hi,

I'm work on a testing framework for Python. Until now I have
implemented the main features of PyUnit and JUnit 4.x. I like the
annotation syntax of JUnit 4.x and it's theory concept is great
therefore you can imagine how my framework will be.

I plan a lot of additionally features which are neither part of Junit
4.5 nor PyUnit. Finding testcases automatically is a good idea.

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


Re: Deleteing empty directories

2009-03-30 Thread CinnamonDonkey
My understanding was that rmtree removes a whole tree not just the
empty directories?

eg.

root
 - file1
 - file2
 - dir1
 - dir2
  - file3
  - dir3

I would expect; dir1 and dir3 to be deleted and nothing else touched.

My attempt came up with:

import os
import shutil

def isDirEmpty( path ):
if not os.path.isdir( path ):
return False

contents = os.listdir( path )

if len(contents) == 0:
return True

return False


def RecurseTree( path ):
if not os.path.isdir( path ):
return False

contents = os.listdir( path )

if len(contents) == 0:
print "Deleting Empty Dir '%s'" % (path,)
#shutil.rmtree(path)

else:
for item in contents:
investigate = "%s\\%s" % (path, item)
if os.path.isdir(investigate):
RecurseTree( investigate )


if __name__ == '__main__':
RecurseTree( r"c:\temp" )


But I'm not sure what the max recursion depth is in python? Plus I
think this could be more efficient.




On 30 Mar, 15:59, Tim Golden  wrote:
> CinnamonDonkey wrote:
> > Hi All,
>
> > I've been scratching my head all afternoon trying to work out the best/
> > quickest way is to delete empty directories within a tree (Windows).
>
> > I've looked at os.walk() but it seems to traverse the directory tree
> > in the wrong order (is it possible to reverse the order?)
>
> > It seems the only way is to manually walk the tree myself recursively
> > and then back up deleteing a directory if it is found to be empty.
>
> In general, the place to look for these things in the
> stdlib is usually shutil. (Slightly awkward that
> "shell" in Windows means everything that happens on
> the desktop, while "shell" in Unix means everything
> *except* what happens on the desktop! This is the
> Unix meaning.)
>
> And sure enough...
>
> """
> rmtree( path[, ignore_errors[, onerror]])
>
> Delete an entire directory tree (path must point to a directory). If 
> ignore_errors is true, errors resulting from failed removals will be ignored; 
> if false or omitted, such errors are handled by calling a handler specified 
> by onerror or, if that is omitted, they raise an exception.
> If onerror is provided, it must be a callable that accepts three parameters: 
> function, path, and excinfo. The first parameter, function, is the function 
> which raised the exception; it will be os.listdir(), os.remove() or 
> os.rmdir(). The second parameter, path, will be the path name passed to 
> function. The third parameter, excinfo, will be the exception information 
> return by sys.exc_info(). Exceptions raised by onerror will not be caught.
>
> """
>
> TJG

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


Re: Deleteing empty directories

2009-03-30 Thread CinnamonDonkey
Revised:

root
   + Dir1
  + Dir2
 + Dir3
   + NotEmptyDir
  File1
   File2

Result:

Root
   + NotEmptyDir
  File1
   File2



---


import os
import shutil

def isDirEmpty( path ):
if not os.path.isdir( path ):
return False

contents = os.listdir( path )

if len(contents) == 0:
return True

return False


def RecurseTree( path ):
print "RecurseTree( %s )" % (path,)

if not os.path.isdir( path ):
print "RecurseTree( %s ) - NOT A DIRECTORY" % (path,)
return 0

contents = os.listdir( path )

for item in contents:
investigate = "%s\\%s" % (path, item)
if os.path.isdir(investigate):
RecurseTree( investigate )

contents = os.listdir( path )

if len(contents) == 0:
print "Deleting Empty Dir '%s'" % (path,)
shutil.rmtree(path)



if __name__ == '__main__':
RecurseTree( r"c:\temp" )


On 30 Mar, 16:14, CinnamonDonkey 
wrote:
> My understanding was that rmtree removes a whole tree not just the
> empty directories?
>
> eg.
>
> root
>  - file1
>  - file2
>  - dir1
>  - dir2
>   - file3
>   - dir3
>
> I would expect; dir1 and dir3 to be deleted and nothing else touched.
>
> My attempt came up with:
>
> import os
> import shutil
>
> def isDirEmpty( path ):
>     if not os.path.isdir( path ):
>         return False
>
>     contents = os.listdir( path )
>
>     if len(contents) == 0:
>         return True
>
>     return False
>
> def RecurseTree( path ):
>     if not os.path.isdir( path ):
>         return False
>
>     contents = os.listdir( path )
>
>     if len(contents) == 0:
>         print "Deleting Empty Dir '%s'" % (path,)
>         #shutil.rmtree(path)
>
>     else:
>         for item in contents:
>             investigate = "%s\\%s" % (path, item)
>             if os.path.isdir(investigate):
>                 RecurseTree( investigate )
>
> if __name__ == '__main__':
>     RecurseTree( r"c:\temp" )
>
> But I'm not sure what the max recursion depth is in python? Plus I
> think this could be more efficient.
>
> On 30 Mar, 15:59, Tim Golden  wrote:
>
> > CinnamonDonkey wrote:
> > > Hi All,
>
> > > I've been scratching my head all afternoon trying to work out the best/
> > > quickest way is to delete empty directories within a tree (Windows).
>
> > > I've looked at os.walk() but it seems to traverse the directory tree
> > > in the wrong order (is it possible to reverse the order?)
>
> > > It seems the only way is to manually walk the tree myself recursively
> > > and then back up deleteing a directory if it is found to be empty.
>
> > In general, the place to look for these things in the
> > stdlib is usually shutil. (Slightly awkward that
> > "shell" in Windows means everything that happens on
> > the desktop, while "shell" in Unix means everything
> > *except* what happens on the desktop! This is the
> > Unix meaning.)
>
> > And sure enough...
>
> > """
> > rmtree( path[, ignore_errors[, onerror]])
>
> > Delete an entire directory tree (path must point to a directory). If 
> > ignore_errors is true, errors resulting from failed removals will be 
> > ignored; if false or omitted, such errors are handled by calling a handler 
> > specified by onerror or, if that is omitted, they raise an exception.
> > If onerror is provided, it must be a callable that accepts three 
> > parameters: function, path, and excinfo. The first parameter, function, is 
> > the function which raised the exception; it will be os.listdir(), 
> > os.remove() or os.rmdir(). The second parameter, path, will be the path 
> > name passed to function. The third parameter, excinfo, will be the 
> > exception information return by sys.exc_info(). Exceptions raised by 
> > onerror will not be caught.
>
> > """
>
> > TJG
>
>

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


Re: Thoughts on language-level configuration support?

2009-03-30 Thread Steven D'Aprano
On Mon, 30 Mar 2009 06:40:00 -0700, jfager wrote:

> I've written a short post on including support for configuration down at
> the language level, including a small preliminary half-functional
> example of what this might look like in Python, available at
> http://jasonfager.com/?p=440.
> 
> The basic idea is that a language could offer syntactic support for
> declaring configurable points in the program.

What's a configuration point? Where do I find them in my programs?



> The language system would
> then offer an api to allow the end user to discover a programs
> configuration service, as well as a general api for providing
> configuration values.

Why is an end user changing variables in my program?


> The included example implements the first bit and hints at the third,
> defining a function that looks up what variable its output will be
> assigned to and tries to find a corresponding value from a configuration
> source.  It's very preliminary, but I hope it gives a flavor of the
> general idea.

Not really. It would help if you explained what problem you are trying to 
solve, what people do now, and how what you suggest would make that 
easier. 

Having read your blog post, I'm going to take a wild stab at guessing 
what you mean:

* Programs often have variables which relate to user-configurable 
options. For example, a basic list files command might include something 
like this:

if sys.argv[1] == '-l':
show_long_listing = True
else:
show_long_listing = False
# ...
# much later...
for filename in directory():
if show_long_listing:
print "%s %s %s" % (permissions(), filename, date())
else:
print filename


* The current solution is for the program author to define the interface 
and the implementation separately. For example, in the above code 
snippet, the command line option '-l' is the interface available to the 
end user, while the variable show_long_listing is the implementation used 
to implement configuration options in the program.


* You propose to expose the variable show_long_listing and it's ilk 
directly to the user, presumable via some service which can interrogate 
the program, discover what "configuration variables" are available, and 
allow the user to stuff random values into them in the hope of getting 
new and exciting bugs^W behaviour.


* This is better than current solutions to the question of getting 
configuration options from the user, like getopt and similar, because... ?


* This would be better with syntactic support rather than a library or 
module because... ?



Am I close?



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


Re: smtplib problem with newly rebuilt Debian/lenny system

2009-03-30 Thread cassiope
On Mar 27, 11:29 am, [email protected] (Aahz) wrote:
> [posted & e-mailed, please respond to newsgroup]
>
> In article ,
>
>
>
> cassiope   wrote:
>
> >In attempting to diagnose the cause, I tried directly executing the
> >lines inside the python2.5 interpreter:
>
> >        import  smtplib
> >        s= smtplib.SMTP('localhost')
>
> >but the second line causes  a traceback:
>
> >        File "", line 1, in 
> >        File "/usr/lib/python2.5/smtplib.py", line 244, in __init__
> >            (code, msg) = self.connect(host, port)
> >        File "/usr/lib/python2.5/smtplib.py", line 310, in connect
> >            raise socket.error, msg
> >        socket.error: (97, 'Address family not supported by protocol')
>
> >This is with exim4 and python2.5 on a newly installed lenny system.
> >No error messages appear in /var/log or /var/log/exim4 directories.
>
> What happens if you
> telnet localhost 25
>
> This looks like a network setup issue.
> --
> Aahz ([email protected])           <*>        http://www.pythoncraft.com/
>
> "At Resolver we've found it useful to short-circuit any doubt and just
> refer to comments in code as 'lies'. :-)"
> --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-3-22

Yes, that's what it was.  Exim4 setup was bungled, so wasn't accepting
localhost
connections.  Fixing that resolved the "python" error.
--
http://mail.python.org/mailman/listinfo/python-list


recursive outline numbering for object trees

2009-03-30 Thread Alia Khouri
Hi,

Here my problem description:

Given the following class:

class Node(object):
def __init__(self, name, children=[], parent=None):
self.name = name
self.level = ''
self.children = children
self.parent = parent

def __repr__(self):
name = self.__class__.__name__
return "<%s %s>" % (self.name, self.level)

def __iter__(self):
yield self
for child in self.children:
child.parent = self
for subchild in iter(child):
yield subchild


and the following example:

tree1 = Node('root[1] ', [
Node('branch [1.1]', [
Node('leaf [1.1.1]', []),
Node('leaf [1.1.2]', []),

]),
Node('branch [1.2]', [
Node('leaf [1.2.1]', []),
Node('leaf [1.2.2]', []),
])
])


I would like to create a walk function which when given the root node
(tree1) automatically sets the correct outline numbering for all its
subnodes.

such that

for i in walk(tree):
print i.level

should give the following output:

1
1.1
1.1.1
1.1.2
1.2
1.2.1
1.2.2

Now, I have scoured the web and found this (http://
www.opensubscriber.com/message/[email protected]/9056939.html)
solution from castironpi, which seems to work nicely:


class up( Exception ): pass
class down( Exception ): pass


def outline():
stack = [1]
while True:
try:
# print 'next'
yield '.'.join(str(s) for s in stack)
stack[-1]+= 1
except down:
# print 'down'
stack.append(1)
except up:
# print 'up'
stack.pop(-1)
stack[-1] += 1


def test_outline():
print
o = outline()
print o.next()
print o.throw(down)
print o.throw(down)
print o.next()
print o.throw(up)
print o.throw(down)
print o.next()

running test_outline() generates the required output so, so I tried to
incorporate the above into my walk function with mixed results
(warning: it's ugly):

from itertools import count

def walk(node, level=outline(), last=None, i=count(1), root=True):
'''tree walker assigns parent attribute, creates numbered outline
'''
if root:
node.level = level.next()
yield node
if last:
node.level = last
last = None
if node.children:
next = level.throw(down)
elif i.next() == 5: # length of nodes (hardcoded here for tsting)
raise StopIteration
else:
next = level.throw(up)
for child in node.children:
child.parent = node
child.level = next or level.next()
if child == node.children[-1]:
last = child.level
for subchild in walk(child, level, last, i, root=False):
yield subchild

works for

tree = Node('root[1] ', [
Node('branch [1.1]', [
Node('leaf [1.1.1]', []),

]),
Node('branch [1.2]', [
Node('leaf [1.2.1]', []),
])
])

but barfs for the required tree1 (above).

I've kinda hit a wall here, so any help would be appreciated.

As an aside, if a solution is possible as an external walk function
would it be possible to work in __iter__?

Best,

AliaK










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


Pyowa Meeting this week

2009-03-30 Thread Mike Driscoll
Hi,

This is just a reminder that we have a Pyowa meeting this week. It
will be held at Durham Center in Ames, IA on the ISU campus from 7-9
p.m. Directions are on the website (www.pyowa.org). Topics include the
following:

1) What PyCon attendees thought of PyCon (likes, dislikes, etc)
2) Code snippets that they'd like to show that they learned about or
created at PyCon
3) I attended a meeting about Python user groups and will share ideas
with how we might improve ours
4) The last 10-20 minutes will probably be devoted to figuring out who
will do what next time.

If you're in the area, come on out! Bring your friends, even if they
think Perl is the coolest thing since sliced bread. We'll set them
straight!

Mike Driscoll
www.pyowa.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Deleteing empty directories

2009-03-30 Thread Steven D'Aprano
On Mon, 30 Mar 2009 08:14:55 -0700, CinnamonDonkey wrote:

> My understanding was that rmtree removes a whole tree not just the empty
> directories?


So it seems:

>>> os.mkdir('die-die-die')
>>> os.mkdir('die-die-die/stuff')
>>> shutil.rmtree('die-die-die')
>>>  

I think what you want is os.removedirs().

>>> os.makedirs('root/die-die-die/empty/empty/empty')
>>> os.listdir('root')
['keep', 'die-die-die']
>>> os.removedirs('root/die-die-die/empty/empty/empty')
>>> os.listdir('root')
['keep']




> def isDirEmpty( path ):
> if not os.path.isdir( path ):
> return False
> 
> contents = os.listdir( path )
> 
> if len(contents) == 0:
> return True
> 
> return False

That can be simplified to:

# untested
def isDirEmpty(path):
return os.path.isdir(path) and not len(os.listdir(path))



> def RecurseTree( path ):
> if not os.path.isdir( path ):
> return False

What if it is a symbolic link to a directory?

> contents = os.listdir( path )
>
> if len(contents) == 0:
> print "Deleting Empty Dir '%s'" % (path,) #shutil.rmtree(path)

Why do you go to the trouble of defining isDirEmpty() and then not use it?

> else:
> for item in contents:
> investigate = "%s\\%s" % (path, item) if
> os.path.isdir(investigate):
> RecurseTree( investigate )

As soon as you start recursively walking over directories, you should use 
os.walk. It will almost certainly do what you want.


> if __name__ == '__main__':
> RecurseTree( r"c:\temp" )
> 
> 
> But I'm not sure what the max recursion depth is in python?

By default in my version:

>>> sys.getrecursionlimit()
1000

but it can be changed.


> Plus I think this could be more efficient.

Probably, but why do you care? The file I/O probably will take 99% of the 
time, and I doubt you can improve that.

Of course I could be wrong, so profile, profile, profile, and find out 
where the time really is being spent.


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


Re: Pyowa Meeting this week

2009-03-30 Thread Mike Driscoll
On Mar 30, 10:33 am, Mike Driscoll  wrote:
> Hi,
>
> This is just a reminder that we have a Pyowa meeting this week. It
> will be held at Durham Center in Ames, IA on the ISU campus from 7-9
> p.m. Directions are on the website (www.pyowa.org). Topics include the
> following:
>
> 1) What PyCon attendees thought of PyCon (likes, dislikes, etc)
> 2) Code snippets that they'd like to show that they learned about or
> created at PyCon
> 3) I attended a meeting about Python user groups and will share ideas
> with how we might improve ours
> 4) The last 10-20 minutes will probably be devoted to figuring out who
> will do what next time.
>
> If you're in the area, come on out! Bring your friends, even if they
> think Perl is the coolest thing since sliced bread. We'll set them
> straight!
>
> Mike Driscollwww.pyowa.org

Oops...I seem to be tired today. The meeting will take place on
Thursday, April 2nd. Sorry about that!

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


distutils, No module named numpy.distutils.fcompiler.conv_template

2009-03-30 Thread Luis Alberto Zarrabeitia Gomez

I'm trying to compile the wrappers for ANN (Approximate Nearest Neighbor) from
http://scipy.org/scipy/scikits/wiki/AnnWrapper, either the main one (scikits) or
the attachment in the main page.

However, the command "python setup.py build" produces the exception:

  "ImportError: No module named numpy.distutils.fcompiler.conv_template"

There is no conv_template in numpy.distutils.fcompiler, but there is one at
numpy.distutils. Grepping for conv_template at /usr/lib/python2.5/distutils/
produces no results, and grepping for it at
/usr/lib/python2.5/site-packages/numpy/ returns only 

==
/[...]/numpy/distutils/command/build_src.py:from numpy.distutils.conv_template
import process_file as process_c_file
/[...]/numpy/distutils/command/build_src.py:   
log.info("conv_template:> %s" % (target_file))
==

i.e, it is being imported from distutils and not from fcompiler. 

What's going on here? It is an ubuntu bug? (intrepid) Am I missing some
packages? This happens regardless of if the ANN library exists or not.

If I try to softlink the distutils/conv_template.py module to the fcompiler
directory, I start getting the same symptoms with other modules:
numpy_distribution, extension, interactive.


K.

P.S: Traceback follows.

ky...@home:~/downloads/ann$ python setup.py build
running build
running scons
customize UnixCCompiler
Found executable /usr/bin/gcc
Traceback (most recent call last):
  File "setup.py", line 41, in 
classifiers = filter(None, classifiers.split("\n")),
  File "/usr/lib/python2.5/site-packages/numpy/distutils/core.py", line 184, in
setup
return old_setup(**new_attr)
  File "/usr/lib/python2.5/distutils/core.py", line 151, in setup
dist.run_commands()
  File "/usr/lib/python2.5/distutils/dist.py", line 974, in run_commands
self.run_command(cmd)
  File "/usr/lib/python2.5/distutils/dist.py", line 994, in run_command
cmd_obj.run()
  File "/usr/lib/python2.5/site-packages/numpy/distutils/command/build.py", line
38, in run
self.run_command('scons')
  File "/usr/lib/python2.5/distutils/cmd.py", line 333, in run_command
self.distribution.run_command(command)
  File "/usr/lib/python2.5/distutils/dist.py", line 993, in run_command
cmd_obj.ensure_finalized()
  File "/usr/lib/python2.5/distutils/cmd.py", line 117, in ensure_finalized
self.finalize_options()
  File "/usr/lib/python2.5/site-packages/numpy/distutils/command/scons.py", line
310, in finalize_options
force = self.force)
  File "/usr/lib/python2.5/site-packages/numpy/distutils/fcompiler/__init__.py",
line 804, in new_fcompiler
load_all_fcompiler_classes()
  File "/usr/lib/python2.5/site-packages/numpy/distutils/fcompiler/__init__.py",
line 715, in load_all_fcompiler_classes
__import__ (module_name)
  File "/usr/lib/python2.5/ihooks.py", line 405, in import_module
m = self.load_tail(q, tail)
  File "/usr/lib/python2.5/ihooks.py", line 458, in load_tail
raise ImportError, "No module named " + mname
ImportError: No module named numpy.distutils.fcompiler.conv_template



-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie





-- 
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba 
http://www.universidad2010.cu

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


Re: complaints about no replies last week

2009-03-30 Thread Steven D'Aprano
On Mon, 30 Mar 2009 07:50:49 -0700, pruebauno wrote:

> I myself asked about how to write a library to efficiently do union and
> intersection of sets containing time intervals some time ago on this
> list and got little to no answers. It is a tricky problem. 

With all the confidence of somebody who doesn't need to solve it, I can 
say, no, it's an easy problem, provided you use the correct data 
structure. What you want is an interval tree:

http://en.wikipedia.org/wiki/Interval_tree


> Since I was
> getting paid I got an O(n*n) solution working. 

Just off the top of my head, I *think* you should be able to get that 
down to O(m * log n) where m is the size of one set and n the size of the 
other. Don't quote me on that though.


> People on this list on the other hand do not get paid

We don't??? Damn! I was expecting a HUGE cheque at the end of the month!


BTW Aaron, I haven't replied to your post about the garbage collector 
because that's one part of Python programing where I cherish my ignorance.



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


Re: Thoughts on language-level configuration support?

2009-03-30 Thread jfager
On Mar 30, 11:17 am, Steven D'Aprano  wrote:
> On Mon, 30 Mar 2009 06:40:00 -0700, jfager wrote:
> > I've written a short post on including support for configuration down at
> > the language level, including a small preliminary half-functional
> > example of what this might look like in Python, available at
> >http://jasonfager.com/?p=440.
>
> > The basic idea is that a language could offer syntactic support for
> > declaring configurable points in the program.
>
> What's a configuration point? Where do I find them in my programs?

A configuration point is what you create when you use this proposed
feature.  You find them in your programs wherever you (or whoever
wrote them) defined them.



> > The language system would
> > then offer an api to allow the end user to discover a programs
> > configuration service, as well as a general api for providing
> > configuration values.
>
> Why is an end user changing variables in my program?

Because you used this construct to tell them they could.  It's not a
hook for every variable, just the ones you want to expose.


> > The included example implements the first bit and hints at the third,
> > defining a function that looks up what variable its output will be
> > assigned to and tries to find a corresponding value from a configuration
> > source.  It's very preliminary, but I hope it gives a flavor of the
> > general idea.
>
> Not really. It would help if you explained what problem you are trying to
> solve, what people do now, and how what you suggest would make that
> easier.

It's the configuration problem.  Right now you would use something
like ConfigParser or optparse to populate some configuration object,
which you would then pass around and extract values from.  This would
provide two advantages over these approaches:  it would separate "what
can be configured" from "the mechanism by which it is configured" -
i.e., I, programmer, don't have to make a decision about what the end
user has to do to give me those values.  And it would allow the
configurable surface of the program to be discoverable; I wouldn't
have to poke through code or documentation that was maintained
separate from the source code.



> Having read your blog post, I'm going to take a wild stab at guessing
> what you mean:
>
> * Programs often have variables which relate to user-configurable
> options. For example, a basic list files command might include something
> like this:
>
> if sys.argv[1] == '-l':
>     show_long_listing = True
> else:
>     show_long_listing = False
> # ...
> # much later...
> for filename in directory():
>     if show_long_listing:
>         print "%s %s %s" % (permissions(), filename, date())
>     else:
>         print filename
>
> * The current solution is for the program author to define the interface
> and the implementation separately. For example, in the above code
> snippet, the command line option '-l' is the interface available to the
> end user, while the variable show_long_listing is the implementation used
> to implement configuration options in the program.
>
> * You propose to expose the variable show_long_listing and it's ilk
> directly to the user, presumable via some service which can interrogate
> the program, discover what "configuration variables" are available, and
> allow the user to stuff random values into them in the hope of getting
> new and exciting bugs^W behaviour.

What, exactly, is preventing them from stuffing random values into
them now?  The boilerplate of manually reading the param from the
command line and assigning it to a variable makes things magically
safe?  If you'll notice in the example code, there's an optional
parameter to process whatever argument is passed in; that's where any
safety checks can be performed.


> * This is better than current solutions to the question of getting
> configuration options from the user, like getopt and similar, because... ?

Because it means the developer doesn't have to know or care how the
end-user ultimately inserts configured values.  Because it provides a
unified mechanism for all configuration instead of a hodge-podge of
different techniques for what is ultimately the same basic task.
Because it lets you quickly make a parameter of a program configurable
(or not) without having to rearchitect the application.



> * This would be better with syntactic support rather than a library or
> module because... ?

Because a big part of this is the discovery of these endpoints, which
seems like it would best be handled at a lower level than a library
can currently provide.  Because finding what variable you're assigning
to currently requires querying the bytecode.  Because libraries
inevitably introduce boilerplate that can simply be avoided if its a
language feature.  Because the requirement is broad enough and similar
enough across concerns to be provided as a basic service.

- Jason

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
Here again is example:


from ctypes import *

class REG_INFO(Structure):
_fields_ = [
('address', c_ubyte),
('message', c_char * 256),
('size', c_ubyte)
]

class myclass(object):
#
# def __new__(class_, init_val, size, reg_info):
def __init__(self, init_val, size, reg_info):

# self = object.__new__(class_)
self.reg_info = reg_info
print self.reg_info.message
self.val = self
self.size = reg_info.size
print 'address = 0x%02X' % self.reg_info.address
# return self
#
def __getitem__(self, index): # gets a single bit
if index >= self.size:
return self.val
return (self.val >> index) & 1
#
def __get__(self): # gets a single bit
return self.val
#
def __setitem__(self,index,value): # sets a single bit
if index >= self.size:
self.val = value
return
value = (value&1L)<
dog val = 0x123456789ABCDEF0
TEST 2
hello world
address = 0xAB
type(cat) = 
Traceback (most recent call last):
  File "C:\path... \bignum5.py", line 62, in 

print 'cat val = 0x%016X' % cat
TypeError: int argument required

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


Problems with code

2009-03-30 Thread Zach Goscha
Hi,

I am trying to call an unbound method (Map.Background) but getting the 
following error:
 TypeError: unbound method background() must be called with Map instance as 
first argument (got nothing instead)

Here is some of the code(not completed) 

Thanks in Advance
- Zach


Code:

class Knight(games.Sprite):
""" A moving knight. """
SWORD_DELAY = 50
sword_wait = 0


def update(self):
""" moving knight based on keys pressed. """

if games.keyboard.is_pressed(games.K_w):
self.y -= 3
self.angle = 0
if games.keyboard.is_pressed(games.K_s):
self.y += 3
self.angle = 180
if games.keyboard.is_pressed(games.K_a):
self.x -= 3
self.angle = 270
if games.keyboard.is_pressed(games.K_d):
self.x += 3
self.angle = 90

if self.top > games.screen.height:
self.bottom = 0

if self.bottom < 0:
self.top = games.screen.height
Map.background()

if self.left > games.screen.width:
self.right = 0

if self.right < 0:
self.left = games.screen.width

class Map(games.Sprite):

def background(self):
new_back = games.load_image("map3.jpg",
transparent = False)
games.screen.background = new_back

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


Re: Upgrade Python on a Mac

2009-03-30 Thread 7stud
On Mar 3, 4:01 am, Graham Dumpleton 
wrote:
> On Mar 3, 8:53 am, Rey Bango  wrote:
>
> > Hi,
>
> > I'd like to upgrade the installed version of Python that came standard
> > on OS X (Leopard) with either 2.6.1 or 3.0.1. Before I stick my foot
> > in it, I just wanted to get a better understanding of the process.
>
> > If I download the disk image installer from 
> > here:http://www.python.org/download/
> > will it allow me to upgrade my existing version or is it more involved
> > (eg: making a new build).
>
> > I've looked through the python.org page for upgrade instructions for a
> >Macand haven't found it.
>
> > Any help would be appreciated.
>
> Beware of the official Python binary installers for MacOS X if wanting
> to do Python web development.
>
> Based on feedback these installers have only been compiled for 32 bit
> architectures. This makes them useless if you want to run mod_python
> or mod_wsgi with Apache that comes with MacOS X as it runs as 64 bit
> and relies on the Python framework having 64 bit, which these
> installers do not provide.
>
> If this is going to affect you, build from source code. Configure
> options required would be, as an example:
>
> ./configure --prefix=/usr/local/python-3.0  \
>  --enable-framework=/usr/local/python-3.0/frameworks \
>  --enable-universalsdk=/ MACOSX_DEPLOYMENT_TARGET=10.5 \
>  --with-universal-archs=all
>


Which of the following is the "official Python binary installer for
MacOS X"?

-
Python-2.6.1.tar.bz2

python-2.6.1-macosx2008-12-06.dmg
-

and is the problem with 3.0 specifically or all versions?


> Note that not all MacPorts installers have been both 32/64 bit either.
> Not sure if they have fixed this issue.
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: recursive outline numbering for object trees

2009-03-30 Thread Gabriel Genellina
Alia Khouri  yahoo.com> writes:

> Given the following class:
> 
> class Node(object):
> def __init__(self, name, children=[], parent=None):
> self.name = name
> self.level = ''
> self.children = children
> self.parent = parent
> 
> def __repr__(self):
> name = self.__class__.__name__
> return "<%s %s>" % (self.name, self.level)
> 
> def __iter__(self):
> yield self
> for child in self.children:
> child.parent = self
> for subchild in iter(child):
> yield subchild
> 
> and the following example:
> 
> tree1 = Node('root[1] ', [
> Node('branch [1.1]', [
> Node('leaf [1.1.1]', []),
> Node('leaf [1.1.2]', []),
> 
> ]),
> Node('branch [1.2]', [
> Node('leaf [1.2.1]', []),
> Node('leaf [1.2.2]', []),
> ])
> ])
> 
> I would like to create a walk function which when given the root node
> (tree1) automatically sets the correct outline numbering for all its
> subnodes.
> 
> such that
> 
> for i in walk(tree):
> print i.level
> 
> should give the following output:
> 
> 1
> 1.1
> 1.1.1
> 1.1.2
> 1.2
> 1.2.1
> 1.2.2
> 
> Now, I have scoured the web and found this (http://
> www.opensubscriber.com/message/python-list  python.org/9056939.html)
> solution from castironpi, which seems to work nicely:

With a slight modification (don't automatically advance in up/down) 
castironpi's code is easier to use in this case:

class up( Exception ): pass
class down( Exception ): pass

def outline():
stack = [1]
while True:
try:
# print 'next'
yield '.'.join(str(s) for s in stack)
stack[-1]+= 1
except down:
# print 'down'
stack.append(0)
except up:
# print 'up'
stack.pop(-1)

def walk(node, o=None):
if o is None:
o = outline()
node.level = o.next()
yield node
if node.children:
o.throw(down)
for child in node.children:
for subnode in walk(child, o):
yield subnode
o.throw(up)

(BTW, there is a proposal for a "yield from" statement, so the last part would 
become:

o.throw(down)
for child in node.children:
yield from walk(child, o)
o.throw(up)

and I think it's a lot clearer)

> As an aside, if a solution is possible as an external walk function
> would it be possible to work in __iter__?

return walk(self)

-- 
Gabriel Genellina


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


Re: Problems with code

2009-03-30 Thread Chris Rebert
2009/3/30 Zach Goscha :
> Hi,
>
> I am trying to call an unbound method (Map.Background) but getting the
> following error:
>  TypeError: unbound method background() must be called with Map instance as
> first argument (got nothing instead)
>
> Here is some of the code(not completed)
>
> Thanks in Advance
> - Zach
>
>
> Code:
> class Knight(games.Sprite):
>     """ A moving knight. """
>     SWORD_DELAY = 50
>     sword_wait = 0
>
>
>     def update(self):
>     """ moving knight based on keys pressed. """

>     if self.bottom < 0:
>     self.top = games.screen.height
>     Map.background()

> class Map(games.Sprite):
>
>     def background(self):
>     new_back = games.load_image("map3.jpg",
> transparent = False)

In the above code, you do `Map.background()`; this is invalid.
background() is an /instance/ method of Map objects, *not* a
classmethod of Map, so you can't call it on the class itself, only
instances of it. In Java-ish terms, you're trying to call a non-static
method like it's a static method.
You either need to make background() a classmethod, or create an
instance of Map to call the method on instead (perhaps you have a
self.map variable and this is just a typo?).

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


Re: modifying a list element from a function

2009-03-30 Thread TP
Adrian Dziubek wrote:

> Could you explain your high level goal for this? It looks like a very
> wicked way of doing things. Have You tried to read the list methods'
> documentation? Maybe there you find something you need (like
> list.index)?

Hello,

I have a "disambiguation" function that modifies a text
contained "somewhere" in each element of a list. The way this text is
accessed by the function should depend on an external accessor, or setter,
so that any list could be processed, in a generic way.

Julien

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list


RE: tkinter questions: behavior of StringVar, etc

2009-03-30 Thread John Posner
Eric Brunel said:

 >> The Tk instance is registered in a hidden variable in the 
 >> Tkinter module. When
 >> you don't specify a master, it'll use the latest created Tk 
 >> instance one by
 >> default. BTW, the latest should be the only one: it is 
 >> quite unsafe to create
 >> several Tk instances in the same application.

Again, "latest" is incorrect for IDLE 2.6.1:

>>> from Tkinter import *
>>> root1 = Tk()
>>> root2 = Tk()
>>> root3 = Tk()
>>> frm = Frame()   # no master specified
>>> frm.master is root3
False
>>> frm.master is root1
True

 >> Well, I personnally don't see any point on doing any master creation
 >> implicitely, so I never use this master auto-creation for 
 >> anything.

+1. "Explicit is better than implicit."

-John





E-mail message checked by Spyware Doctor (6.0.0.386)
Database version: 5.12070
http://www.pctools.com/en/spyware-doctor-antivirus/
--
http://mail.python.org/mailman/listinfo/python-list


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread MRAB

[email protected] wrote:

Here again is example:


from ctypes import *

class REG_INFO(Structure):
_fields_ = [
('address', c_ubyte),
('message', c_char * 256),
('size', c_ubyte)
]

class myclass(object):
#
# def __new__(class_, init_val, size, reg_info):
def __init__(self, init_val, size, reg_info):

# self = object.__new__(class_)
self.reg_info = reg_info
print self.reg_info.message
self.val = self


Don't you really mean:

self.val = init_val


self.size = reg_info.size
print 'address = 0x%02X' % self.reg_info.address
# return self

[snip]

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


Re: Problems with code

2009-03-30 Thread Albert Hopkins
On Mon, 2009-03-30 at 11:05 -0500, Zach Goscha wrote:
> Hi,
>  
> I am trying to call an unbound method (Map.Background) but getting the
> following error:
>  TypeError: unbound method background() must be called with Map
> instance as first argument (got nothing instead)
>  
> Here is some of the code(not completed) 
>  
> Thanks in Advance
> - Zach
>  
>  
> Code:
> 
> class Knight(games.Sprite):
> """ A moving knight. """
> SWORD_DELAY = 50
> sword_wait = 0
> 
> 
> def update(self):
> """ moving knight based on keys pressed. """
>  
> if games.keyboard.is_pressed(games.K_w):
> self.y -= 3
> self.angle = 0
> if games.keyboard.is_pressed(games.K_s):
> self.y += 3
> self.angle = 180
> if games.keyboard.is_pressed(games.K_a):
> self.x -= 3
> self.angle = 270
> if games.keyboard.is_pressed(games.K_d):
> self.x += 3
> self.angle = 90
>  
> if self.top > games.screen.height:
> self.bottom = 0
>  
> if self.bottom < 0:
> self.top = games.screen.height
> Map.background()
>  
> if self.left > games.screen.width:
> self.right = 0
>  
> if self.right < 0:
> self.left = games.screen.width
>  
> class Map(games.Sprite):
>  
> def background(self):
 
> new_back = games.load_image("map3.jpg",
> transparent = False)
> games.screen.background = new_back

'self' implies that this is an instance method.  If this is not an
instance method then don't use self.


If what you want is a static method (or class method) then use the
staticmethod (or classmethod) function or decorator.

I.e.

@staticmethod
def background():
...


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

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


Re: Ordered Sets

2009-03-30 Thread pataphor
On Mon, 30 Mar 2009 03:30:04 -0500
Nick Craig-Wood  wrote:

> >>> class Node(object):
> ... __slots__ = ["prev", "next", "this"]
> ... def __init__(self, prev, next, this):
> ... self.prev = prev
> ... self.next = next
> ... self.this = this

[...]

> So the Node class actually takes less memory 38 Mbytes vs 53 Mbytes
> for the list.
 
Well OK, but if we're going to start optimizing, it seems we don't need
to store the key itself in the Node or the list. Here's a version of my
script that stores only prev and next. Another twist is that it is now
possible to arbitrarily set the starting point for the iteration. (Just
in case someone needed a cue for further ranting)

P.

import collections

class OrderedSet(collections.MutableSet):
'Set that remembers the order elements were added'
# Big-O running times for all methods are the same as for regular
sets. 
def __init__(self, iterable=None):
self.links = {} # key --> [prev,next]
if iterable is not None:
self |= iterable

def __len__(self):
return len(self.links)

def __contains__(self, key):
return key in self.links

def add(self, key):
if not self:
self.start = key
self.links = {key: [key,key]}
elif key not in self.links:
links = self.links
start = self.start
prev, next  = links[start]
links[prev][1] = key
links[start][0] = key
links[key] = [prev,start]

def discard(self, key):
links = self.links
if key in links:
prev,next = links.pop(key)
if self.links:
links[prev][1] = next
links[next][0] = prev
if key  == self.start:
self.start = next
else:
del self.start

def __iter__(self):
links = self.links
start = self.start
if links:
yield start
prev,next = links[start]
while next != start:
yield next
prev,next = links[next]

def __reversed__(self):
links = self.links
start = self.start
if links:
prev,next = links[start]
while prev != start:
yield prev
prev,next = self.links[prev]
yield start

def pop(self, last=True):
if not self:
raise KeyError('set is empty')
key = next(reversed(self)) if last else next(iter(self))
self.discard(key)
return key

def __repr__(self):
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self))

def __eq__(self, other):
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
return not self.isdisjoint(other)

def test():
D = OrderedSet('abcd')
R = OrderedSet()
for x in list(D):
print(D,R)
R.add(D.pop(last = False))
print(D,R)
print()
R.start = 'c'
print(list(reversed(R)))

if __name__ == '__main__':
test()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re. suid/sudo in python

2009-03-30 Thread rustom
On Mar 30, 6:47 pm, [email protected] wrote:
> On Mar 30, 1:16 pm, Rustom Mody  wrote:
>
>
>
> > Ben Finney wrote
>
> > > The key thing to realise is that, having relinquished privilege, the same 
> > > process can't get it back again as easily. So if you need to
> > > do some tasks as a privileged user, do those *very* early and then drop 
> > > the privileges for the rest of the life of the process.
>
> > > Taking this further, you should isolate exactly what tasks need root 
> > > privilege into a separate process altogether, and make
> > > that process as well-tested and simple as possible: it should do nothing 
> > > *but* those tasks for which it needs root privilege.
>
> > I dont think this would be easy or convenient (if at all possible) in my 
> > case.
>
> > I am trying to write a tiny web based application that will give an
> > overall picture of LVM, Volume groups, Raid, SCSI and the underlying
> > disk partitions. The administrative tools dealing with low level
> > storage stack (e.g. fdisk, pvcreate, vgcreate, lvcreate, mdadm etc.)
> > need to be run as root.
>
> > However since this runs behind apache. Apache creates a separate user
> > for the webserver. Hence the CGI scripts or any other tools that they
> > call run as that user.
>
> > The solution currently is
> > - Write the CGI program in C, put setuid(0), setgid(0) statements in
> > that file and then perform any other actions (including calling other
> > scripts)
> > - Set the S bit of the executable of the CGI binary compiled from the
> > C file (chmod +S xxx.cgi)
>
> > Yeah yeah "Security! HOLE!!" etc but please note that this is running
> > on linux on vmware on an otherwise secure system.
>
> > So whats the best way of doing this in python?
>
> Have a 'server process' running with root privilege ( a script started
> by a privileged account)  and implement a protocol to ask for system
> info from your cgi scripts under apache. In python this is a lot
> easier than it sounds.
> The simplest case would be that to send a 'system command' to the
> server through a unix socket, the server
> executes the command as received and returns the command output. Not
> more than a day work, I believe. Not much more secure that
> a setuid python script, also, maybe less :-)

Well the current C root owned setuid-ing and calling out to shell is
simple enough I guess.
The shell could be replaced by python of course.

> A better implementation would be such that the protocol only allows
> for a set of pre-defined safe requests ...

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread Scott David Daniels

[email protected] wrote:

...
It seems like there's no way to do what I'm trying.  I am confined to
Python 2.5.3 for business reasons.

So I want a class ShadowRegister, which just has a value that I can do
get/set bit sel and slice ops.  I got that working with __init__.  It
was subclass from "object".  Then I wanted a RegisterClass that was a
subclass of ShadowRegister, which would read a hardware register
before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
but when I try to print in hex format ('0x016X') it said it required
an int (but the ShadowRegister class had no issues).  Then I was told
instead of using object I could subclass as long (seemed the only
solution for Python 2.5).  Then when I started to want to add my own
init code (like register length in bits), I was told I should use
__new__ instead of __init__.  So but ever since then I've noticed that
my value is not changing from the initially set value.  I'm really
cornfused now.


OK, you have asked questions at narrow points about how you can get
whatever is your local problem at the moment, and have received answers
addressing your question.  In effect, you've asked 'Which way to the
apothecary?' and 'How do I get around this building' without mentioning
that you're looking for how to get to the top of Everest.  Perhaps you
even mentioned your whole goal on your first post, but people see the
questions they see.

You cannot subclass immutable values to get mutable values; a number
of the properties of immutables are what allows them to participate
in such things as sets and dictionaries.  Such things aren't reasonable
for mutable Registers, unless the identity of the register is the thing
that distinguishes it.  A set of three Registers, all of which currently
have the value 5 stored in them had better be a 3-element set, or when
you change one of them without changing the other two the size of your
set will have to magically change.

On a recent job, I was dealing with registers in a device and bit
accesses for reads and writes.  I made a pair of classes:  One to
correspond to the device itself (with only one instance per chunk of
hardware attached), and another class representing possible values of
the register at given moments.  The RegisterValue class was a subclass
of int, and had a from_name class method, mask (&) and set (|)
operations and a repr that show the non-zero bits connected by '|'.
The Register class had read, read_masked, write, and write_masked
operations.  It worked quite well.  Actually, there were more than
a hundred RegisterValue classes, most of which were subclasses of the
base RegisterValue class with different bit name lists.

Remember that just because a Register might have a name, doesn't mean
its string representations don't necessarily reflect the contents of
the register (lists show their contents, for example).  I think you'll
do better to separate the idea of a register and its contents.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re. suid/sudo in python

2009-03-30 Thread Martin P. Hellwig

Rustom Mody wrote:

I don't think this is necessary a python problem but a generic, SUID 
scripts considered dangerous, problem.


The essence of your program is that you only want information, that is 
great! Since that makes it all a bit simpler.


How about writing a cronjob that outputs the require information into a 
file and then write a separate program that reads the output and 
displays it on the web site when requested?


If you are particular adventurous, your cronjob could check if a certain 
file exists first and then execute the (CPU intensive) script, this is 
especially handy when you expect changes every second or so. I wouldn't 
write that check for file existence in Python though, you don't want to 
fire up an interpreter every second. But a simple sh script will do only 
calling python if it really needs too (perhaps also putting it on an 
appropriate nice level).


hth

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


Re: running pyhton IDLE on windows vista

2009-03-30 Thread Scott David Daniels

ryan wrote:

I am facing  problems running python25 on vista . i was able to
successfully install it but when i try to run it then,
its throws errors saying Firewall issues ..

I tried disabling the firewall but no go..


Yes, we believe you.  Read "smart questions"
http://www.catb.org/~esr/faqs/smart-questions.html

We need more information.
You can oen a command window (run cmd.exe) and type

C:> python -m idlelib.idle

This may give details about exactly what is happening in
output in that command window.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: email from windows

2009-03-30 Thread Suraj Barkale
prakash jp  gmail.com> writes:
> Hi all,
>  
> In windows environment, how to send email from one gmail address to another
gmail (or another mail) addrress
>  
>  
Gmail requires SSL smtp support which is missing in Python stdlib. However, you
can look at this example (http://www.example-code.com/python/gmail_smtp_465.asp)
which uses a module called chilkat to do the work.

Regards,
Suraj

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


Syntax disagreement between IDLE and pydev?

2009-03-30 Thread Jim Garrison

IDLE (3.1a1) accepts

a,*b = re.split(str,pattern)

and does the right thing ('a' gets the first result and 'b' gets
the rest).

pydev configured to use the exact same Python 3.1a1 runtime doesn't
like this syntax (in my source, column 23 is the asterisk):

Encountered "*" at line 32, column 23. Was expecting one of: 
 ... ";" ... "=" ...
 "+=" ... "-=" ... "*=" ... "/=" ... "//=" ... "%=" 
... "&=" ... "|=" ... "^=" ...
 "<<=" ... ">>=" ... "**=" ... "lambda" ... "not" ... 
   "+" ... "-" ... "~" ... "(" ...
 "[" ... "{" ... "False" ... "True" ... "None" ... 
 ...  ...
  ...  ...  ...  ... 
"\'" ... "\"" ...
 "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" 
... "\"\"\"" ...


Can I assume pydev is wrong or am I missing something?
--
http://mail.python.org/mailman/listinfo/python-list


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread David Bolen
[email protected] writes:

> class myclass(object):
> #
> # def __new__(class_, init_val, size, reg_info):
> def __init__(self, init_val, size, reg_info):
>
> # self = object.__new__(class_)
> self.reg_info = reg_info
> print self.reg_info.message
> self.val = self

Note that here you assign self.val to be the object itself.  Are you
sure you didn't mean "self.val = init_val"?

> (...)
> def __int__(self):
> return self.val

Instead of an integer, you return the current class instance as set up
in __init__.  The __int__ method ought to return an integer.

> def __long__(self):
> return long(self.val)

And this will be infinite recursion, since long() will try to
call the __long__ method on  so you're just recursing on the
__long__ method.

You can see this more clearly with:

>>> cat = myclass(0x55, 32, my_reg)
>>> int(cat)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: __int__ returned non-int (type myclass)
>>>

I won't post the traceback for long(cat), as it's, well, "long" ...

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
On Mar 30, 9:40 am, Scott David Daniels  wrote:
> [email protected] wrote:
> > ...
> > It seems like there's no way to do what I'm trying.  I am confined to
> > Python 2.5.3 for business reasons.
>
> > So I want a class ShadowRegister, which just has a value that I can do
> > get/set bit sel and slice ops.  I got that working with __init__.  It
> > was subclass from "object".  Then I wanted a RegisterClass that was a
> > subclass of ShadowRegister, which would read a hardware register
> > before doing get bit sel/slices, or read HW reg, do set bit sel/slice,
> > but when I try to print in hex format ('0x016X') it said it required
> > an int (but the ShadowRegister class had no issues).  Then I was told
> > instead of using object I could subclass as long (seemed the only
> > solution for Python 2.5).  Then when I started to want to add my own
> > init code (like register length in bits), I was told I should use
> > __new__ instead of __init__.  So but ever since then I've noticed that
> > my value is not changing from the initially set value.  I'm really
> > cornfused now.
>
> OK, you have asked questions at narrow points about how you can get
> whatever is your local problem at the moment, and have received answers
> addressing your question.  In effect, you've asked 'Which way to the
> apothecary?' and 'How do I get around this building' without mentioning
> that you're looking for how to get to the top of Everest.  Perhaps you
> even mentioned your whole goal on your first post, but people see the
> questions they see.
>
> You cannot subclass immutable values to get mutable values; a number
> of the properties of immutables are what allows them to participate
> in such things as sets and dictionaries.  Such things aren't reasonable
> for mutable Registers, unless the identity of the register is the thing
> that distinguishes it.  A set of three Registers, all of which currently
> have the value 5 stored in them had better be a 3-element set, or when
> you change one of them without changing the other two the size of your
> set will have to magically change.
>
> On a recent job, I was dealing with registers in a device and bit
> accesses for reads and writes.  I made a pair of classes:  One to
> correspond to the device itself (with only one instance per chunk of
> hardware attached), and another class representing possible values of
> the register at given moments.  The RegisterValue class was a subclass
> of int, and had a from_name class method, mask (&) and set (|)
> operations and a repr that show the non-zero bits connected by '|'.
> The Register class had read, read_masked, write, and write_masked
> operations.  It worked quite well.  Actually, there were more than
> a hundred RegisterValue classes, most of which were subclasses of the
> base RegisterValue class with different bit name lists.
>
> Remember that just because a Register might have a name, doesn't mean
> its string representations don't necessarily reflect the contents of
> the register (lists show their contents, for example).  I think you'll
> do better to separate the idea of a register and its contents.
>
> --Scott David Daniels
> [email protected] Hide quoted text -
>
> - Show quoted text -

Thanks Scott.  I think you are saying don't try to subclass from type
long, because it is immutable (cannot be changed).  If I subclass from
type object, then the value can be changed, but I still can't print
without explicitely casting back to long in the main routine.  Is this
a catch 22 scenario, or am I just overlooking some method of long to
override to change it's return value?

Here is what I currently have morphed to:

""" Works to change self.val but not self"""

from ctypes import *

class REG_INFO(Structure):
_fields_ = [
('address', c_ubyte),
('message', c_char * 256),
('size', c_ubyte),
('init', c_ulong)
]

class BigNumber(long):
def __new__(class_, init_val, size):
print 'printing size: %d' % size
self = long.__new__(class_, init_val)
self.size = size
self.val = self
return self
#
def __repr__(self):
print 'from __repr__: %s' % self
return '%s(%s)' % (type(self).__name__, self)
#
def __getitem__(self, index): # gets a single bit
if index >= self.size:
return self.val
return (self.val >> index) & 1
#
def __get__(self): # gets a single bit
return self.val
#
def __setitem__(self,index,value): # sets a single bit
if index >= self.size:
self.val = value
return
value = (value&1L)<
bird = 0x9ABCDEF0

TEST 3
bird.val = 0x9ABCDEF0
bird = 0x9ABCDEF0
bird[0] val = 0x0
bird.val = 0x9ABCDEF1
bird = 0x9ABCDEF0 <== Value did not change (because immutable
I guess)
bird[0] val = 0x1







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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
On Mar 30, 10:53 am, David Bolen  wrote:
> [email protected] writes:
> > class myclass(object):
> > #
> >     # def __new__(class_, init_val, size, reg_info):
> >     def __init__(self, init_val, size, reg_info):
>
> >         # self = object.__new__(class_)
> >         self.reg_info = reg_info
> >         print self.reg_info.message
> >         self.val = self
>
> Note that here you assign self.val to be the object itself.  Are you
> sure you didn't mean "self.val = init_val"?
>
> > (...)
> >     def __int__(self):
> >         return self.val
>
> Instead of an integer, you return the current class instance as set up
> in __init__.  The __int__ method ought to return an integer.
>
> >     def __long__(self):
> >         return long(self.val)
>
> And this will be infinite recursion, since long() will try to
> call the __long__ method on  so you're just recursing on the
> __long__ method.
>
> You can see this more clearly with:
>
>     >>> cat = myclass(0x55, 32, my_reg)
>     >>> int(cat)
>     Traceback (most recent call last):
>       File "", line 1, in 
>     TypeError: __int__ returned non-int (type myclass)
>     >>>
>
> I won't post the traceback for long(cat), as it's, well, "long" ...
>
> -- David

Hi David;

Yep I had fixed up that version actually.  Here is the latest.

from ctypes import *

class REG_INFO(Structure):
_fields_ = [
('address', c_ubyte),
('message', c_char * 256),
('size', c_ubyte)
]

class myclass(object):
#
# def __new__(class_, init_val, size, reg_info):
def __init__(self, init_val, reg_info):

# self = object.__new__(class_)
self.reg_info = reg_info
print reg_info.message
self.val = init_val
self.size = reg_info.size
self.addr = reg_info.address
print 'address = 0x%02X' % self.addr
# return self
#
def __getitem__(self, index): # gets a single bit
if index >= self.size:
return self.val
return (self.val >> index) & 1
#
def __get__(self): # gets a single bit
return self.val
#
def __setitem__(self,index,value): # sets a single bit
if index >= self.size:
self.val = value
return
value = (value&1L)<
dog val = 0x123456789ABCDEF0
TEST 2
hello world
address = 0xAB
type(cat) = 
cat val = 0x0055
TEST 3
hello world
address = 0xAB
type(bird) = 
Traceback (most recent call last):
  File "C:\(big path)\bignum5.py", line 69, in 
print 'bird val = 0x%016X' % bird
TypeError: int argument required
--
http://mail.python.org/mailman/listinfo/python-list


Re: please include python26_d.lib in the installer

2009-03-30 Thread Johan Compen
On Sat, Mar 28, 2009 at 1:17 AM, Mark Hammond  wrote:
>> Please note: I want to build my own code in Debug mode for debugging.
>> I don't want to build or use the debug version of Python. I also can't
>
> Python does this on purpose so you don't accidentally mix different versions
> of the C runtime library.  This would happen ff you defined DEBUG in your
> code but use Python built without DEBUG - Python using a different name for
> its lib prevents this.
>
> Note that just shipping the _d.lib wouldn't help - you would need the _d.dll
> itself, plus *all* extension modules you use - *all* .pyd/.dll etc files
> have the trailing _d, and a debug version of Python refuses to load the
> release versions without the _d.
>
> I'd recommend leaving DEBUG etc disbled, but enable debug information and
> disable optimizations while debugging.

If Python doesn't include the _d.lib file, then why does the header
file reference it? I would prefer manual control over which lib file
to use. (And I don't want to disable _DEBUG for other reasons).

Could the header file be changed so it alwas uses the release lib? In
most cases it's actually ok to mix different versions of the CRT in
one application. See the following blog post about this issue:

http://www.davidlenihan.com/2008/01/choosing_the_correct_cc_runtim.html

The goal is to use one runtime library throughout your entire application.

That is nearly impossible since you typically don't have control of
which runtime library other libraries use.

It turns out is is OK to mix runtime libraries *except* in certain
cases. A well written library should avoid these cases and then it
doesn't matter if the runtime libraries match. Libraries that cannot
avoid these cases should ship with 4 versions of their libraries that
match the 4 versions of the runtime libraries.


P.S. If pyconfig.h really wanted to use the correct CRT, then it would
need to reference  different lib files for both VS2005 and 2008.

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


Re: Syntax disagreement between IDLE and pydev?

2009-03-30 Thread Mike Driscoll
On Mar 30, 12:40 pm, Jim Garrison  wrote:
> IDLE (3.1a1) accepts
>
>         a,*b = re.split(str,pattern)
>
> and does the right thing ('a' gets the first result and 'b' gets
> the rest).
>
> pydev configured to use the exact same Python 3.1a1 runtime doesn't
> like this syntax (in my source, column 23 is the asterisk):
>
> Encountered "*" at line 32, column 23. Was expecting one of:
>  ...     ";" ...     "=" ...
>   "+=" ...     "-=" ...     "*=" ...     "/=" ...     "//=" ...     "%="
> ...     "&=" ...     "|=" ...     "^=" ...
>   "<<=" ...     ">>=" ...     "**=" ...     "lambda" ...     "not" ...
>     "+" ...     "-" ...     "~" ...     "(" ...
>   "[" ...     "{" ...     "False" ...     "True" ...     "None" ...
>  ...      ...
>    ...      ...      ...      ...
>      "\'" ...     "\"" ...
>   "\'\'\'" ...     "\"\"\"" ...     "\'" ...     "\"" ...     "\'\'\'"
> ...     "\"\"\"" ...
>
> Can I assume pydev is wrong or am I missing something?

If it works that way in IDLE and from the command line python, then
it's almost proof positive that pydev goofed up.

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
If I cast it long then it prints fine, but I was hoping there would be
a slicker way (to accomplish this in the class itself).

print 'bird val = 0x%016X' % long(bird)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax disagreement between IDLE and pydev?

2009-03-30 Thread Jim Garrison

Jim Garrison wrote:

IDLE (3.1a1) accepts

a,*b = re.split(str,pattern)

and does the right thing ('a' gets the first result and 'b' gets
the rest).

pydev configured to use the exact same Python 3.1a1 runtime doesn't
like this syntax (in my source, column 23 is the asterisk):

Encountered "*" at line 32, column 23. Was expecting one of:  
... ";" ... "=" ...
 "+=" ... "-=" ... "*=" ... "/=" ... "//=" ... "%=" 
... "&=" ... "|=" ... "^=" ...
 "<<=" ... ">>=" ... "**=" ... "lambda" ... "not" ...
"+" ... "-" ... "~" ... "(" ...
 "[" ... "{" ... "False" ... "True" ... "None" ... 
 ...  ...
  ...  ...  ...  ... 
"\'" ... "\"" ...
 "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" 
... "\"\"\"" ...


Can I assume pydev is wrong or am I missing something?


I should add that this seems to affect only the occurrence of the
syntax error marker (red X and red underline). The code runs
correctly.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread Dave Angel
After taking out the class  myclass stuff, the code worked for me in 
Python 2.6.1, through the cat line.  Could you please tell us what 
version of Python you're running this on?


import sys
print "Python version: ", sys.version

yielded (on my machine)
Python version:  2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 
32 bit (Intel)]


(There are two typos in  Test 3, and in Test 4 you seem to be treating 
this object like a list.)


[email protected] wrote:

On Mar 30, 10:53 am, David Bolen  wrote:
  

[email protected] writes:


class myclass(object):
#
# def __new__(class_, init_val, size, reg_info):
def __init__(self, init_val, size, reg_info):
  
# self =bject.__new__(class_)

self.reg_info =eg_info
print self.reg_info.message
self.val =elf
  

Note that here you assign self.val to be the object itself.  Are you
sure you didn't mean "self.val =nit_val"?



(...)
def __int__(self):
return self.val
  

Instead of an integer, you return the current class instance as set up
in __init__.  The __int__ method ought to return an integer.



def __long__(self):
return long(self.val)
  

And this will be infinite recursion, since long() will try to
call the __long__ method on  so you're just recursing on the
__long__ method.

You can see this more clearly with:

>>> cat =yclass(0x55, 32, my_reg)
>>> int(cat)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: __int__ returned non-int (type myclass)
>>>

I won't post the traceback for long(cat), as it's, well, "long" ...

-- David



Hi David;

Yep I had fixed up that version actually.  Here is the latest.

from ctypes import *

class REG_INFO(Structure):
_fields_ =
('address', c_ubyte),
('message', c_char * 256),
('size', c_ubyte)
]

class myclass(object):
#
# def __new__(class_, init_val, size, reg_info):
def __init__(self, init_val, reg_info):

# self =bject.__new__(class_)
self.reg_info =eg_info
print reg_info.message
self.val =nit_val
self.size =eg_info.size
self.addr =eg_info.address
print 'address =x%02X' % self.addr
# return self
#
def __getitem__(self, index): # gets a single bit
if index >=elf.size:
return self.val
return (self.val >> index) & 1
#
def __get__(self): # gets a single bit
return self.val
#
def __setitem__(self,index,value): # sets a single bit
if index >=elf.size:
self.val =alue
return
value =value&1L)<
dog val =x123456789ABCDEF0
TEST 2
hello world
address =xAB
type(cat) =class '__main__.myclass'>
cat val =x0055
TEST 3
hello world
address =xAB
type(bird) =class '__main__.myclass'>
Traceback (most recent call last):
  File "C:\(big path)\bignum5.py", line 69, in 
print 'bird val =x%016X' % bird
TypeError: int argument required

  

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


Re: please include python26_d.lib in the installer

2009-03-30 Thread Christian Heimes
Johan Compen wrote:
> If Python doesn't include the _d.lib file, then why does the header
> file reference it? I would prefer manual control over which lib file
> to use. (And I don't want to disable _DEBUG for other reasons).
> 
> Could the header file be changed so it alwas uses the release lib? In
> most cases it's actually ok to mix different versions of the CRT in
> one application. See the following blog post about this issue:

No, the header files can't be changed. Python debug builds have a
different ABI than release builds of Python. The _DEBUG flags enables
the debugging ABI with additional checks.

If you *really* want to use a debug build of Python then you can easily
build your own.

Christian

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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
On Mar 30, 11:53 am, Dave Angel  wrote:
> After taking out the class  myclass stuff, the code worked for me in
> Python 2.6.1, through the cat line.  Could you please tell us what
> version of Python you're running this on?
>
> import sys
> print "Python version: ", sys.version
>
> yielded (on my machine)
> Python version:  2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500
> 32 bit (Intel)]
>
> (There are two typos in  Test 3, and in Test 4 you seem to be treating
> this object like a list.)
>


Python version:  2.5.1 (r251:54863, Apr 18 2007, 08:51:08)
(Thought it was 2.5.3, sorry).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax disagreement between IDLE and pydev?

2009-03-30 Thread Benjamin Kaplan
On Mon, Mar 30, 2009 at 2:23 PM, Jim Garrison  wrote:

> Jim Garrison wrote:
>
>> IDLE (3.1a1) accepts
>>
>>a,*b = re.split(str,pattern)
>>
>> and does the right thing ('a' gets the first result and 'b' gets
>> the rest).
>>
>> pydev configured to use the exact same Python 3.1a1 runtime doesn't
>> like this syntax (in my source, column 23 is the asterisk):
>>
>> Encountered "*" at line 32, column 23. Was expecting one of:  ...
>> ";" ... "=" ...
>>  "+=" ... "-=" ... "*=" ... "/=" ... "//=" ... "%="
>> ... "&=" ... "|=" ... "^=" ...
>>  "<<=" ... ">>=" ... "**=" ... "lambda" ... "not" ...
>>  "+" ... "-" ... "~" ... "(" ...
>>  "[" ... "{" ... "False" ... "True" ... "None" ... 
>> ...  ...
>>   ...  ...  ...  ...
>>  "\'" ... "\"" ...
>>  "\'\'\'" ... "\"\"\"" ... "\'" ... "\"" ... "\'\'\'" ...
>> "\"\"\"" ...
>>
>> Can I assume pydev is wrong or am I missing something?
>>
>
> I should add that this seems to affect only the occurrence of the
> syntax error marker (red X and red underline). The code runs
> correctly.
>

Pydev does its own syntax checking. You probably have the grammar version
set to 2.5 or 2.6, so the syntax checker doesn't like it, but then run it
through the Python 3 interpreter. Go to the project's properties and under
Pydev- Project Type, change the grammar version to 3.0.


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


Re: global name 'self' is not defined - noob trying to learn

2009-03-30 Thread mark . seagoe
On Mar 30, 12:01 pm, [email protected] wrote:
> On Mar 30, 11:53 am, Dave Angel  wrote:
>
> > After taking out the class  myclass stuff, the code worked for me in
> > Python 2.6.1, through the cat line.  Could you please tell us what
> > version of Python you're running this on?
>
> > import sys
> > print "Python version: ", sys.version
>
> > yielded (on my machine)
> > Python version:  2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500
> > 32 bit (Intel)]
>
> > (There are two typos in  Test 3, and in Test 4 you seem to be treating
> > this object like a list.)
>
> Python version:  2.5.1 (r251:54863, Apr 18 2007, 08:51:08)
> (Thought it was 2.5.3, sorry).

This part is probably important
Python version:  2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310
32 bit (Intel)]

Not sure what you mean that you removed the class myclass.  Not sure
how it could work.

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


Re: recursive outline numbering for object trees

2009-03-30 Thread Alia K
Thanks Gabriel. Your solution works like a charm. (-:

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


Re: Syntax disagreement between IDLE and pydev?

2009-03-30 Thread Fabio Zadrozny
>>> IDLE (3.1a1) accepts
>>>
>>>    a,*b = re.split(str,pattern)
>>>
>>> and does the right thing ('a' gets the first result and 'b' gets
>>> the rest).
>>>
>>> pydev configured to use the exact same Python 3.1a1 runtime doesn't
>>> like this syntax (in my source, column 23 is the asterisk):
>>>
>>> Encountered "*" at line 32, column 23. Was expecting one of: 
>>> ...     ";" ...     "=" ...
>>>  "+=" ...     "-=" ...     "*=" ...     "/=" ...     "//=" ...     "%="
>>> ...     "&=" ...     "|=" ...     "^=" ...
>>>  "<<=" ...     ">>=" ...     "**=" ...     "lambda" ...     "not" ...
>>>  "+" ...     "-" ...     "~" ...     "(" ...
>>>  "[" ...     "{" ...     "False" ...     "True" ...     "None" ... 
>>> ...      ...
>>>   ...      ...      ...      ...
>>>  "\'" ...     "\"" ...
>>>  "\'\'\'" ...     "\"\"\"" ...     "\'" ...     "\"" ...     "\'\'\'" ...
>>>     "\"\"\"" ...
>>>
>>> Can I assume pydev is wrong or am I missing something?
>>

Tthis has already been fixed for 1.4.5 (provided you set your project
as python 3.0). You can get the nightly and check it (as that should
be the released version:
http://pydev.blogspot.com/2009/03/new-pydev-release-soon-call-for-testers.html
)

Cheers,

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


Re: i have to change default tab length in pydev

2009-03-30 Thread Fabio Zadrozny
> Most such problems are caused by mixing tabs and spaces in the same file.
>  Pick one style and be consistent, and you'll be in good shape.
>
> My approach is to always expand tabs.  My tab key simply gets me to a
> convenient column, there never are any tabs in my source files.
>
> (I don't use either notepad++ or pydev, but most editors I have used or
> written can be configured to use spaces)
>

Just as a note, both notepad++ and pydev do support using spaces for tabs.

Cheers,

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


  1   2   >