[FAQTS] Python Knowledge Base Update -- June 22nd, 2000

Fiona Czuczman fiona at sitegnome.com
Thu Jun 22 18:21:17 EDT 2000


Hi Guys,

Below are the entries that made it into http://python.faqts.com

I sent this mail yesterday but it doesn't seem to have made it to the 
newsgroup... so I send again.  Sorry if it turns up in duplicate.

regards,

Fiona


## New Entries #################################################


-------------------------------------------------------------
Is there a Win32 module that I can use to set directory privileges, that has the functionality of cacls.exe using python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3914
-------------------------------------------------------------
Fiona Czuczman
Mark Hammond, Albert Hopkins

Check the win32security module plus the pywintypes module for direct 
access to security objects.

The "problem" is that Python makes no attempt to make it easier than it 
is in C/C++.  There is a fair bit of magic to weave, involving a number 
of steps to successfully create the ACLs, ACEs and SDs!  Getting the 
code wrong can cause a few problems ;-)

There are no "nice" wrappers around this stuff simply because I 
personally dont have the experience with them.  The few times I need to 
do security related things I search MSDN for C sample code, and 
translate it to the relevant win32security/pywintypes calls.

As an example of the verbosity required, below is some code from the 
book examples <plug>Chapter 16 - Windows NT Administration</plug> that 
simply creates a security descriptor ready to be applied to the 
necessary object.
(In fact, this is probably a reasonable percentage of what you need)

# A utility function that creates an NT security object for a user.
def CreateUserSecurityDescriptor(userName):
    sidUser = win32security.LookupAccountName(serverName, userName)[0]
    sd = win32security.SECURITY_DESCRIPTOR()

    # Create the "well known" SID for the administrators group
    subAuths = ntsecuritycon.SECURITY_BUILTIN_DOMAIN_RID, \
               ntsecuritycon.DOMAIN_ALIAS_RID_ADMINS
    sidAdmins = win32security.SID(ntsecuritycon.SECURITY_NT_AUTHORITY,
subAuths)

    # Now set the ACL, giving user and admin full access.
    acl = win32security.ACL(128)
    acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, sidUser)
    acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, sidAdmins)

    sd.SetSecurityDescriptorDacl(1, acl, 0)
    return sd


-------------------------------------------------------------
Is there a help or man function, like in unix-like OS, that we can use to describe any function of python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3910
-------------------------------------------------------------
Fiona Czuczman
Richard Chamberlain, William Dandreta

You can use docstrings which are there for most things.

import sys
print sys.__doc__

-------------

There is a Windows help file called Pythlp.chm that I use. You can 
download from the python webside. It has an index and you can seach for 
keywords.


-------------------------------------------------------------
How do I get Tkinter to resize frames etc. when their parents (e.g. a Toplevel instance) are resized?
http://www.faqts.com/knowledge-base/view.phtml/aid/3913
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain, John Grayson

It depends on the kind of manager you use, but I'll use pack as an 
example.

from Tkinter import *
root=Tk()
Frame(root,borderwidth=3,relief=GROOVE).pack(side=LEFT,fill=BOTH,expand=
YES)
root.mainloop()

If you look at www.manning.com/grayson you can download two chapters of
Python and Tkinter programming one of which is screen layout. (of course
you'll be much better buying a copy ;)


You'll also find over 20,000 lines of example code at the same site...


-------------------------------------------------------------
Is there some documentation for the python interface to postgresql?
http://www.faqts.com/knowledge-base/view.phtml/aid/3911
-------------------------------------------------------------
Fiona Czuczman
Rod Haper,Erno Kuusela

The PyGreSQL package ftp://ftp.druid.net/pub/distrib/PyGreSQL.tgz
(Latest released version) has a tutorial directory.

The README that comes with the module documents the postgresql-specific 
api. the most recent versions support DB-API also.


-------------------------------------------------------------
Why does 123.4+2=125.40000000000001?
http://www.faqts.com/knowledge-base/view.phtml/aid/3909
-------------------------------------------------------------
Fiona Czuczman
Ruud de Rooij

>From http://www.python.org/1.6/

  The repr() of floating point numbers now gives full (17-digit)
  precision. This means that a number like 8.1 (which cannot be
  represented exactly in binary) will show up as 8.0999999999999996 when
  displayed using repr(). This shouldn't break any code but it may
  confuse users, especially since the interactive shell uses repr() to
  display all floating point results. Use str() or explicit precision in
  string format (e.g. "%.12g" % x) to see the the more familiar, rounded
  form.


## Edited Entries ##############################################


-------------------------------------------------------------
How can I get the current date and time?
How do I print out the current time?
http://www.faqts.com/knowledge-base/view.phtml/aid/3600
-------------------------------------------------------------
Mike Hostetler, Fiona Czuczman
http://www.python.org/doc/current/lib/module-time.html,Jeff

I didn't think that the official documentation was very clear on this 
(but it is there), so I'm adding it here.

If you do

>> import time
>> print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))

You'll see something like:

2000-06-08 15:05:09

To dissect this:

time.time() gets the time in seconds since the epoch

time.localtime(secs) creates a tuple

time.strftime(string,tuple) creates a string of the time, using the 
formatting in "string" (that is what the "%"'s are doing there).
To see the full table of formatting options, see 
http://www.python.org/doc/current/lib/module-time.html

Also, the system man page, ('man strftime')? documents a closer set of 
the formatting options actually supported by the libc, which is what the 
python interpreter actually calls into. this will come into play mostly 
on 'older' machines, but python runs on them too!







More information about the Python-list mailing list