Python Knowledge Base Update -- June 20th, 2000 
Author Message
 Python Knowledge Base Update -- June 20th, 2000

Hi guys,

Below are the latest entries into http://www.*-*-*.com/

cheers,

Fiona Czuczman

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

-------------------------------------------------------------
Is getstatusoutput() the prefered method to run os commands from within a python script or is there something more generic?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Matthew Schinckel

Try using os.system(command)

-------------------------------------------------------------
How can I find out how much memory a remote computer has installed? The computers are on the same NT network and have the same login.
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Roger Upole

Try the win32pdh module from the win32 extensions.  The NT performance
monitor can access memory stats on another machine if you have
sufficient privilege.  The hard part is getting the path string right,
but there is a path browser to help with that.  I think it's
win32/lib/win32pdhutil.py.

-------------------------------------------------------------
How do I create a fully stand alone programme to put on systems where python is not installed?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Matthias Huening, Matthew Schinckel

You should try Gordon McMillan's "Installer". It's at:
http://www.*-*-*.com/

One option is to use mxCGIPython (can't remember the URL, find it on
Parnassus < http://www.*-*-*.com/ ;, and just have a compiled
single-file binary and the program.

-------------------------------------------------------------
I'm trying to get a user's username and password but by using raw_input() the password is echoed to the screen.  How can I make it so that their password isn't shown?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Janko Hauser, David Goodger

Look for the module getpass in the library reference. It also supports
windows according to the document string.

    import getpass
    password = getpass.getpass()

If you still see the password echoed to the screen, then you're missing
some functionality, probably termios. Check out the getpass.py code for
the decision tree that is followed.

-------------------------------------------------------------
Could someone give me a simple explanation of regular expressions?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Andrew Kuchling

Regular expressions are a mini-language for defining a certain set
of strings; the re module then takes a regular expression and tells you
if a given string matches the expression.  For example, [abc] means
'a', 'b', or 'c'; \bA.*?\b means a word beginning with 'A' (\b is a
special sequence meaning "match a word boundary").  As you can see,
regular expressions are concise but can easily be obscure.

Regular expressions will practically never be faster than a simple
string.find, so if you're searching for a fixed string, don't use
regular expressions; their extra expressive power costs you in
performance.  But they can do things that aren't possible with the
string module alone, and would require several lines of Python code
to implement.

Consult the Library Reference, or the Regular Expression HOWTO for
more info.  ( http://www.*-*-*.com/ ,
http://www.*-*-*.com/ )

-------------------------------------------------------------
Can two threads get in each others' way if they simultaneously try to mutate a list?
Can two threads get in each others' way if they simultaneously try to mutate a list?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Aahz Maruch

It depends how they mutate it.  I believe that single builtin functions
(e.g. [].append()) are thread-safe.  OTOH, [].sort() may not be
thread-safe, particularly if you've got user-defined classes on the list
with __cmp__() defined.

-------------------------------------------------------------
When and why should I use Intvar() etc?
When and how do I use the textvariable option?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain

from Tkinter import *
root=Tk()
myIntVar = IntVar()
myStringVar= StringVar()
Label(root,textvariable=myStringVar).pack(side=TOP)
def setLabel():
    myStringVar.set(myIntVar.get())
Radiobutton(root,text="First Radiobutton",variable=myIntVar,value=1,
                    command=setLabel).pack(side=LEFT)
Radiobutton(root,text="Second Radiobutton",variable=myIntVar,value=2,
                    command=setLabel).pack(side=LEFT)
root.mainloop()

I'm using both IntVar and textvariable in this example.

I create new instances of Intvar and StringVar. I then create a label
and assign the textvariable option to the instance of StringVar. I can
now set the text of this label by called myStringVar.set('Hello') or get
the text by myStringVar.get().

Next I've created two Radiobuttons and assigned their variable options
to the instance of IntVar. I give them two separate values. Any changes
to the radiobuttons will now be reflected in myIntVar.

I created a function called setLabel which uses the set and get methods
of the instances. I then assign it to the label by called
myStringVar.set(myIntVar.get()) which changes the value of the
textvariable and the change is immediately reflected on the label.

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

-------------------------------------------------------------
How can I extract all the keys in a formatting string to a list?
How can I extract all the keys in a formatting string to a list?
http://www.*-*-*.com/
-------------------------------------------------------------
Nathan Wallace, Fiona Czuczman
Michael Str?der, Johannes Stezenbach

For example, go from:

  '%(language)s has %(count)03d quote types.' % vars()

to get:

  ['language','count']

Try something like this:

------------
class GrabKeys:
    def __init__(self):
        self.keys = []
    def __getitem__(self, name):
        self.keys.append(name)
        return 0 # 0 is apparently compatible with all % format
characters

gk = GrabKeys()

'%(language)s has %(count)03d quote types.' % gk

print gk.keys
------------

-------------------------------------------------------------
Are colons unnecessary in my Python code?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Courageous, David Porter, Fran?ois Pinard, Moshe Zadka, Dirck Blaskey

Lead in discussion:

Sometimes colons seem syntactically unnecessary. For example:

        if <condition>:
                statement
        else:
                statement

Really, else doesn't need a colon, as far as I can tell (I can see the
need for the if, supposing you want to have the statement on the same
line).

Answer(s):

- When using python-mode in Emacs (or jed), the colon facilitates
auto-indention. Also, if you forget the colon, the next line will not be
indented, so you will catch your mistake.

- Granted, but it is always good for a language to have a bit of
redundant information.  When properly thought, such redundancy prevents
various mistakes from programmers (once they are used to it, of course
:-), and often increase overall legibility.

- Theoretically, a colon is only necessary in things like

if yes: print "yes"

Since otherwise the parser can figure out when to stick a colon.
However, usability studies show people are more comfortable when the
beginning of a block is signaled, and I can see why:

if yes
        print "yes"

Seems....{*filter*}. Much less readable then

if yes:
        print "yes"

Guido didn't want 10 ways (or even 2) to spell things, so the colon is
mandated for all.

- > if yes: print "yes"

Oddly enough, the parser doesn't really need the colon here either.
It can manage to figure out where the if expression ends without it.

The colon is almost entirely for readability purposes.
(there are a couple of places where ambiguity occurs without it).

If you're curious about the other thread,
or about Python without colons,
or to test my above assertion,
check out:

http://www.*-*-*.com/

-------------------------------------------------------------
What is a tuple and why is it used (as opposed to an array, etc)?
http://www.*-*-*.com/
-------------------------------------------------------------
Fiona Czuczman
Stephen Hansen, Aahz Maruch, Warren Postma,Russell E Owen

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 ...

read more »



Sat, 07 Dec 2002 03:00:00 GMT  
 Python Knowledge Base Update -- June 20th, 2000

Hello Python Folk!

Below are the entries I've entered into http://python.faqts.com today.

cheers,

Fiona Czuczman

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

-------------------------------------------------------------
What module would I use to make serial I/O?
http://www.faqts.com/knowledge-base/view.phtml/aid/3888
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain

Have a look at ftp.python.org/pub/python/contrib/sio-151.zip

-------------------------------------------------------------
Is there a Python module that has functions in it to manipulate Dbase tables and indexes?
http://www.faqts.com/knowledge-base/view.phtml/aid/3890
-------------------------------------------------------------
Fiona Czuczman
Anders M Eriksson

You don't say which version of dBASE and not on which platform you
are, so let's assume that you are using Windows 9x/NT/2000.

The only real alternative today is to use ODBC!

The win32-all comes with an ODBC modules that works OK, if you want to
spice it up a bit get mxODBC (seach on Vaults of Parnassus)

-------------------------------------------------------------
How can I convert a hex number to an integer?
http://www.faqts.com/knowledge-base/view.phtml/aid/3886
-------------------------------------------------------------
Fiona Czuczman
Jarkko Torppa, Thomas Wouters

Quote:
>>> eval('0x32')
50
>>> import string

# Forces base to 16(hex)
Quote:
>>> string.atoi('32',16)

50
# Guesses base from leading characters

Quote:
>>> string.atoi('0x32',0)

Note that in Python 1.6, you can do the above two things like this:

Quote:
>>> int('0x32',16)
50
>>> int('0x32',0)

50

-------------------------------------------------------------
How do I get a file list via an ftp connection?
http://www.faqts.com/knowledge-base/view.phtml/aid/3887
-------------------------------------------------------------
Fiona Czuczman
Thomas Weholt

I think you could do either a ls or dir, like so :

ftp = ftplib.... # open connection
file_list = ftp.dir()
or file_list = ftp.ls()

Just off the top of my head. Check the source of ftllib, it`s almost
self-explanatory.

-------------------------------------------------------------
Is anyone aware of a Computer Algebra System (CAS) i.e a mathematical software that is able to derivate, integrate, simplify, find limits, analytically - in python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3885
-------------------------------------------------------------
Fiona Czuczman
Konrad Hinsen, Jonathan Epstein, Matt Feinstein

Pythonica isn't there yet, but it's an interesting start. More at:

       http://www.strout.net/python/pythonica.html

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

Matlab has an interface to Maple (their "Symbolic Toolbox", I think).

It might be useful to study their API, and emulate it in a reasonable
(and hopefully legal) fashion in Python.

It's pretty simple, at least from the user's point of view. You
declare your symbolic variables a la

sym x y z;

and then just go ahead and use the symbolic variables in the
interpreter in algebraic or arithmetic expressions the same way you
would use numerical expressions-- all the arithmetic and algebraic
operators used by the interpreter are appropriately overloaded. There
is an accompanying suite of functions and state variables that control
how the symbolic stuff is done. There is also an 'Extended' symbolic
toolbox that gives the user direct access to the Maple kernel.

## 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

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

Quote:
>> 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



Sun, 08 Dec 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Python Knowledge Base Update -- September 20th, 2000

2. Python Knowledge Base Update -- September 20th, 2000

3. Python Knowledge Base Update -- May 20th, 2000

4. Python Knowledge Base Update -- June 28th, 2000

5. Python Knowledge Base Update -- June 26th, 2000

6. Python Knowledge Base Update -- June 24th, 2000

7. Python Knowledge Base Update -- June 22nd, 2000

8. Python Knowledge Base Update -- June 16th, 2000

9. Python Knowledge Base Update -- June 15th, 2000

10. Python Knowledge Base Update -- June 12th, 2000

 

 
Powered by phpBB® Forum Software