[FAQTS] Python Knowledge Base Update -- June 16th, 2000
Fiona Czuczman
fiona at sitegnome.com
Fri Jun 16 07:10:56 EDT 2000
Hi!
My arrangement with FAQTS was to get 225 entries into
http://python.faqts.com by June 16. I've done it! With a lot of help
from you guys. Thanks everyone.
This doesn't mean my daily entries will cease, I'm going to continue
working on the FAQTS Python Knowledge base for a while to come. Its fun
and educational :-)
Regards,
Fiona Czuczman
## New Entries #################################################
-------------------------------------------------------------
How can I map a drive using pythonwin, and see if it was successful or not?
http://www.faqts.com/knowledge-base/view.phtml/aid/3791
-------------------------------------------------------------
Fiona Czuczman
Fred Gansevles, Roger Upole
This code written using the 'dynwin' package.
# wnet.py -- wnet module
from dynwin import winwin, winclass, msgloop, windll
from dynwin.structob import struct_object
from dynwin.oracle import Oracle
mpr = windll.module ('mpr')
RESOURCETYPE_DISK = 0x00000001
class NETRESOURCE (struct_object):
oracle = Oracle (
'NETRESOURCE',
'Lllllllll',
('dwScope',
'dwType',
'dwDisplayType',
'dwUsage',
'lpLocalName',
'lpRemoteName',
'lpComment',
'lpProvider')
)
def use(drive, host, user, passwd):
nr = NETRESOURCE()
nr.dwType = RESOURCETYPE_DISK
lpLocalName = windll.cstring(drive)
lpRemoteName = windll.cstring(r'\\%s\%s' % (host, user))
lpProvider = windll.cstring('Microsoft Windows Network')
nr.lpLocalName = lpLocalName.address()
nr.lpRemoteName = lpRemoteName.address()
nr.lpProvider = lpProvider.address()
return mpr.WNetAddConnection2(nr,
windll.cstring(passwd), windll.cstring(user), 0)
def unuse(drive):
return mpr.WNetCancelConnection2(windll.cstring(drive), 1, 1)
------------
You should be able to use the COM interface for Windows Scripting host.
import win32com.client
wnt=win32com.client.Dispatch('Wscript.Network')
wnt.MapNetworkDrive('z:','//someserver/someshare')
To test it, just try to access the drive:
os.listdir('z:') or something similar.
-------------------------------------------------------------
What is a tuple and why is it used (as opposed to an array, etc)?
http://www.faqts.com/knowledge-base/view.phtml/aid/3795
-------------------------------------------------------------
Fiona Czuczman
Stephen Hansen, Aahz Maruch, Warren Postma
A Tuple is of a fixed length, the length it was when it was created. A
list is of a dynamic length -- its actual size, I believe, from a
memory-ish standpoint, that lists are actually larger then they appear
to be in the Python environment. On the C level, my understanding from
reading various posts is that the list allocates additinoal memory for
elements which may be added in later, so that it doesn't have to
allocate One Piece Of Memory every time an element is added. A Tuple is
immutable. Once created, with its given values, it can not be changed. a
= (1,2,3) can not have a[1] = 5 done. A list is mutable. You can change
the individual elements at any time.
My unknowing guess? The overhead to create the truly 'dynamic' lists is
completely wasted in enough circumstances that it warrented making an
array which was fixed, in both size, and content.
----------
>From my POV, the primary purpose of tuples is for dictionaries: they
allow you to do things like
foo = {}
foo[(1,2)] = 3
This essentially permits the use of multi-column primary keys (to use
database terminology). In addition, as Stephen Hansen pointed out,
tuples are a fair bit more efficient than lists.
---------
I first came across the term tuple in relation to Relational Algebra and
in particular, in the realm of SQL and Databases.
A row of data in a database is often called a tuple. Really, it's a way
of creating a Structure like you would do in C, very efficiently, which
once created, is immutable. This means you can create a hash on it, and
then use the hash to get the piece of data out of a database table. This
is exactly how Tuples work, and they are hashed and then placed into an
associative array (Python calls associative arrays Dictionaries).
So whereas there is one native Array type in C, the List type in Python
is a mutable dynamically sized array, and a Tuple is a fixed immutable
grouping of items, and a Dictionary is like an array where the key
doesn't have to be a number. So, three kinds of things which have
something in common with a C array is better than just having a return
to the Bad Old Days where C arrays were fixed in size, mutable to the
point that you could easily crash your system, and totally
non-polymorphic.
-------------------------------------------------------------
Where should I put my HTMLgen directory?
How do I add a path to sys.path?
http://www.faqts.com/knowledge-base/view.phtml/aid/3796
-------------------------------------------------------------
Fiona Czuczman
Jeff Kunce
When you do an "import" python looks for the module in all the
directories in sys.path
Below are four ways to let python know where you have installed HTMLgen.
I use #4 myself.
1) Add the directory to sys.path in your program before you import the
module:
import sys
sys.path.append('C:\\Program Files\\Python\\Lib\\HTMLgen')
2) Add the directory to your systems "PYTHONPATH" environment
variable before you start your program. For windows:
SET PYTHONPATH=C:\Program Files\Python\Lib\HTMLgen
or if PYTHONPATH is already defined:
SET PYTHONPATH=C:\Program Files\Python\Lib\HTMLgen;%PYTHONPATH%
3) Create a file 'C:\\Program Files\\Python\\HTMLgen.pth' containing the
single line: Lib/HTMLgen
4) Import HTMLgen as a package
a) create an empty file C:\\Program
Files\\Python\\Lib\\HTMLgen\__init__.py
b) in your program, import HTMlgen like this:
from HTMLgen import HTMLgen
All these procedures are explained in the documentation, and in the
various python books. None is specific to HTMLgen.
And, personally, I would not put HTMLgen in the 'Lib' directory - I
think of that as modules that are distributed with python, and may get
overwritten with a new release. I'd suggest installing it as
C:\Program Files\Python\local\HTMLgen
or just
C:\Program Files\Python\HTMLgen
-------------------------------------------------------------
I have an ascii string like 0C44DH. Is there a Python function to convert it to decimal?
http://www.faqts.com/knowledge-base/view.phtml/aid/3799
-------------------------------------------------------------
Fiona Czuczman
Donn Cave
Yes, everything except that trailing H anyway. I'd use
string.atoi('c44d', 16), and I guess in the end that is about the same
thing as int('c44d', 16)
-------------------------------------------------------------
How do I get the current index of the insertion character in an Entry widget?
http://www.faqts.com/knowledge-base/view.phtml/aid/3789
-------------------------------------------------------------
Fiona Czuczman
Hans-Joachim Widmaier
After digging in Tk docs for some time I've finally found how to get the
current index of the insertion character in an Entry widget:
self.index("insert")
will do the trick. Fair enough, "insert" isn't listed in the INDICES
section, just in a note (in the Text widget man page).
## Edited Entries ##############################################
-------------------------------------------------------------
What is the Python code that will print primes below 1000?
http://www.faqts.com/knowledge-base/view.phtml/aid/2823
-------------------------------------------------------------
Fiona Czuczman
François Pinard,Emile van Sebille
initial answer:
import re
for n in range(2, 1000):
if not re.match('(11+)\\1+$', '1'*n):
print n
and then again:
While the above submission is an interesting
application of regular expressions, it's not really
the way to get a list of primes. Below is primes.py,
taken from the standard distribution that is faster
by a factor of 10-20:
primesuspects = range(3,1000,2)
ptr = 0
while ptr < len(primesuspects):
val = primesuspects[ptr]
for i in primesuspects[ptr+1:]:
if not i % val:
primesuspects.remove(i)
ptr = ptr + 1
print primesuspects
More information about the Python-list
mailing list