Re: Place the timer in Python script

2009-09-18 Thread Avell Diroll

Vamsikrishna wrote:

How to place the timer in python code.


timeit ...

it's in the standard library:

http://docs.python.org/library/timeit.html

Cheers,

Ju
--
Whomever controls the press, owns history
   -- Johann Gutenberg
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python SSH interface

2009-09-04 Thread Avell Diroll

Mag Gam wrote:

Is there something similar to NetSSH
(http://search.cpan.org/dist/Net-SSH-Perl/) for python?



I don't know much about perl modules functionalities, but paramiko might 
be what you are searching for.

http://www.lag.net/paramiko/

Cheers

Ju
--
Those who do not understand Unix are condemned to reinvent it, poorly.
-Henry Spencer
--
http://mail.python.org/mailman/listinfo/python-list


Re: Video?

2009-09-03 Thread Avell Diroll

David C Ullrich wrote:
...

Is that correct? If so is there some other standard Python
windowing kit that does include some sort of video functionality?

(Talking Ubuntu Linux if it matters.)


I don't know about video and wxpython, but gstreamer has some python 
bindings (python-gst0.10 on jaunty).

You may find a nice intro here:
http://pygstdocs.berlios.de/


Hope this helped

Ju
--
True friends stab you in the front.
--
http://mail.python.org/mailman/listinfo/python-list


Re: write iso 9660 with python

2007-11-21 Thread Avell Diroll
bryan rasmussen wrote:
 Hi,
 
 I need to read in a system of files and write them to an iso 9660, any
 libraries suited to this task that are up to date? Python 2.4 or 2.5
 should be assumed.

You could subprocess a call to cdrecord ...
There are also python bindings for libburn ...
http://icculus.org/burn/
but i never tested them

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


Re: Bytes/File Size Format Function

2007-06-13 Thread Avell Diroll
Ben Finney wrote:
 The symbol for bit is 'b'. The symbol for byte is 'B'. 'kb' is
 'kilobit', i.e. 1000 bits. 'mb' is a metre-bit, a combination of two
 units. And so on. The SI units have definitions that are only muddied
 by misusing them this way.

I have to disagree: 'mb' should stand for milli-bit  :)
which could be considered as the probability of a bit
... this might be useful for quantum computing.

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


Re: SSH File Transfer Protocol or SFTP

2006-12-11 Thread Avell Diroll
Lad wrote:
 Is there a module in Python available that I can use for uploading
 files via
   SFTP (SSH File Transfer Protocol)?
 Or do you think that FTP protocol for files uploading  is OK?
 Thank you for replies
 Lad.
 

I believe there are many of those, personally  i am using paramiko :

http://www.lag.net/paramiko/

HIH

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


Re: dictionaries - returning a key from a value

2006-09-01 Thread Avell Diroll
Michael Malinowski wrote:
(snip)
 However, I am curious to know if its possible to get the key from giving
 a value (basically the opposite of what I did above, instead of getting
 a value from a key, I want the key from a value). Is there a way of
 doing this? Or would I need to cycle all the keys until I hit a value
 match (which seems somewhat cumbersome).
(snip)

I believe you need to cycle through the entire dict (but that's what a
dict.method would do ... wouldn't it?) ... but it is really quickly
done using list comprehension (in Ipython shell here):

In [30]: sampledict={'the Holy Grail':'1975', 'Life of Brian':'1979',
'Party Political Broadcast':'1974','Mr. Neutron':'1974',
'Hamlet':'1974', 'Light Entertainment War':'1974'}

In [31]: sampledict Out[31]:
{'Hamlet': '1974',
 'Life of Brian': '1979',
 'Light Entertainment War': '1974',
 'Mr. Neutron': '1974',
 'Party Political Broadcast': '1974',
 'the Holy Grail': '1975'}

In [32]: sampledict.get('the Holy Grail') Out[32]: '1975'

In [33]: sampledict['Mr. Neutron'] Out[33]: '1974'

In [34]: keys = [key for key in sampledict if sampledict[key] == '1974']
In [35]: keys
Out[35]:
['Mr. Neutron',
 'Hamlet',
 'Party Political Broadcast',
 'Light Entertainment War']




HIH

Avell

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


Re: Newbie question about numpy

2006-08-24 Thread Avell Diroll
Paul Johnston wrote:
(snip)
 I noted the lack of matrices so installed numpy
(snip)
 _
 from numpy  import *
 
 a = array([[1,2,3],[4,5,6],[1,2,3]])
 b = array([[1,3,6],[2,5,1],[1,1,1]])
(snip)
 print a * b is \n, a * b
 _
(snip)
 a * b is
 [[ 1  6 18]
  [ 8 25  6]
  [ 1  2  3]]
 _
 
 
 I know its a long time since my degree but that's not matrix
 multiplication is it ?

You consider that a and b are matrices, but for the python interpreter
they are arrays so a*b returns the multiplication of 2 arrays.

For matrices multiplication, you could get a hint by typing the
following in the interpreter :

 import numpy
 dir(numpy)
 help(numpy.matrixmultiply)#type q to exit

which could make you want to try the following code :

 from numpy  import *
 a = array([[1,2,3],[4,5,6],[1,2,3]])
 b = array([[1,3,6],[2,5,1],[1,1,1]])
 print matrixmultiply(a,b)

...
output :
...

array([[ 8, 16, 11],
   [20, 43, 35],
   [ 8, 16, 11]])
...

HIH,
avell

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


Re: String Formatting

2006-08-10 Thread Avell Diroll
OriginalBrownster wrote:
 Hi there:
 
 I was wondering if its at all possible to search through a string for a
 specific character.
 
 I want to search through a string backwords and find the last
 period/comma, then take everything after that period/comma
 
 Example
 
 If i had a list:bread, butter, milk
 
 I want to just take that last entry of milk. However the need for it
 arises from something more complicated.
 
 Any help would be appreciated
 



Would that work for you ?

 a = 'bread, butter, milk'
 a
'bread, butter, milk'
 b = a.split(',')
 b
['bread', ' butter', ' milk']
 c = b[-1]
 c
' milk'
 d = c.strip()
 d
'milk'




HIH


Avell

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


Re: web searching scripts

2006-08-04 Thread Avell Diroll
[EMAIL PROTECTED] wrote:
 Does anyone know of a freely available script that can take a given URL
 and follow every link within it?
 
 Ideally, I would like to start with this to build a quick application
 to grab all the content off a website to publish it to a CD.
 
 Thanks,
 
 jul
 


If you just want to download websites (i.e. not necessarily writing a
program yourself to do that), you may try Httrack, it might suite your
needs.

http://www.httrack.com/

There even seem to be some sort of python bindings ...

http://www.satzbau-gmbh.de/staff/abel/httrack-py/

But there might be some more pythonic solution around ... i would start
looking at twisted or cherrypy, but i never used them myself ...

HIH

regards

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


Re: Reinstalling Python Problem (Newbie)

2006-08-02 Thread Avell Diroll
beno wrote:
 I intend to do that. However, I think this is the RIGHT list to ask 
 questions pertinent to python...

I didn't intend to be abrupt, ... I was just in an hurry, sorry for 
that. Anyway I still see this problem more as a program not compiling 
correctly on Freebsd than python not compiling correctly on a platform 
... everybody has his own point of view :-)

Anyway I don't have any freebsd box around any more, so there is no way 
for me to reproduce your problem (hence my remark about the place to post).

Another thing, if you just want to use zope, it seems to be included in 
freebsd ports ... so you should not need to compile anything to _run_ 
zope on your system, but maybe you need specific options not included in 
the ports (this is only fully supported on freebsd-stable and 
freebsd-current which are AFAIK respectively 6.1 and 7.).
Ref.:
http://www.freebsd.org/ports/zope.html
http://www.freebsd.org/ports/lang.html#python-2.4.3

Zope is not supported in 5.5 anymore but it is present in the ports archive.
ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/5.5-RELEASE/ports/

To get back to your specific problem :

 2 tests failed:
test_mimetools test_urllib2
 48 tests skipped:
test_aepack test_al test_applesingle test_asynchat test_bsddb
test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk
test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses
test_doctest test_fork1 test_gdbm test_gl test_imgfile test_imp
test_linuxaudiodev test_logging test_macfs test_macostools
test_nis test_normalization test_ossaudiodev test_pep277
test_plistlib test_queue test_scriptpackages test_socket
test_socket_ssl test_socketserver test_sunaudiodev test_tcl
test_thread test_threaded_import test_threadedtempfile
test_threading test_threading_local test_threadsignals
test_timeout test_unicode_file test_urllib2net test_urllibnet
test_winreg test_winsound
 13 skips unexpected on freebsd5:
test_threadedtempfile test_imp test_threaded_import test_fork1
test_threading test_threadsignals test_socket test_thread
test_queue test_asynchat test_doctest test_threading_local
test_logging
 *** Error code 1
 
 What do I do about the problems with mimetools and urllib2?

This is the last report of the 'make test' command and there should be a 
few lines before that stating each test one by one and printing problems 
has they appear, and sometime pointing at a possible source for the 
specific problem. Have you got such a report for test_urllib2 ?

 What is meant by pointing to this folder thus (as the installation 
 instructions instruct):
 ./configure --prefix=/usr/python

The autotools (configure/make/make install) default installation 
directory is /usr/local by using the prefix option, you are indicating 
that you want to install all the python files in /usr/python when you 
will execute the command 'make install' (instead of /usr/local). I don't 
know which installation instructions you are following, but FWIK this is 
not  a standard place for python to be installed (although it is 
recommended in the README for the AtheOS platform ...).

 BTW, the modpython site is down!

It was up when I sent my first mail, and it is up as I send this one.
http://www.modpython.org/

 TIA,
 beno
 

As I stated before I don't have any freebsd box to play with ...
HTH

Regards

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


Re: Reinstalling Python Problem (Newbie)

2006-08-02 Thread Avell Diroll
beno wrote:
 Avell Diroll wrote:
 beno wrote:
  

*** tidying a little ***


 What do I do about the problems with mimetools and urllib2?
 

 This is the last report of the 'make test' command and there should be
 a few lines before that stating each test one by one and printing
 problems has they appear, and sometime pointing at a possible source
 for the specific problem. Have you got such a report for test_urllib2 ?
   
 Here it is for both:
 
 test_mimetools
 test test_mimetools failed -- Traceback (most recent call last):
  File /usr/local/zope/py243/Lib/test/test_mimetools.py, line 30, in
 test_boundary
nb = mimetools.choose_boundary()
  File /usr/local/zope/py243/Lib/mimetools.py, line 130, in
 choose_boundary
hostid = socket.gethostbyname(socket.gethostname())
 gaierror: (8, 'hostname nor servname provided, or not known')
 
 
 test_urllib2
 test test_urllib2 failed -- Traceback (most recent call last):
  File /usr/local/zope/py243/Lib/test/test_urllib2.py, line 352, in
 test_file
for url in [
 gaierror: (8, 'hostname nor servname provided, or not known')
 
 Looks like the same error ;) So, where am I supposed to provide this
 servname?
 
  

*** tidying a little ***

This gethostname() problem in the test suite looks like an old problem.

I believe your problem is related to what Anthony Baxter and Guido van
Rossum where discussing back in 2004 on the python-dev mailing list :
http://mail.python.org/pipermail/python-dev/2003-November/040501.html

To put things short the test suite might fail on any test using
socket.gethostname() on a machine where /etc/hostname and /etc/hosts are
not matching.
If that is the case for your machine, the problem is coming from the
test suite, not your python build.

regards,

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


Re: Reinstalling Python Problem (Newbie)

2006-08-01 Thread Avell Diroll
beno wrote:
 It's been years since I've done this. I had a programmer working for me
 who disappeared one day, and now I'm taking over his responsibilities. I
 need to re-configure Apache for mod_python which means I have to rebuild
 python. I'm working with the latest distro. I'm heavily dependent on
 zope, so all this has to work together. Platform is FreeBSD 5.? I have
 the following questions:
 
 What is meant by pointing to this folder thus:
 ./configure --prefix=/usr/python
 
 When I run make test I get these errors:
 
*** errors ***
 I've googled this with no luck. Please advise what to do or at least how
 to start
 TIA,
 beno


I believe this is not the best place to ask your question, ...
you should try :
* the mod_python mailing list : http://www.modpython.org/
* some freebsd resources (e.g. http://www.freebsd.org/community.html )
* some zope resources ( http://www.zope.org/Resources/MailingLists )

A few google hints anyway :
http://www.google.com/search?q=freebsd+mod_python+zopehl=en :
http://www.bsdforums.org/forums/archive/index.php/t-1390.html
http://www.modpython.org/pipermail/mod_python/2004-June/015722.html
http://codespeak.net/pipermail/railroad-dev/2005-April/49.html
http://www.modpython.org/FAQ/faqw.py?req=showfile=faq02.011.htp

I don't have any freebsd system right now so I can't test anything, but
there's two suggestions from those resources :

* put the following lines in /usr/local/sbin/envvars :
LD_PRELOAD=/usr/lib/libc_r.so
export LD_PRELOAD

OR

* compile python without thread support

There seems to be those kind of question on the mod_python mailing list
(even on their FAQ), so I would _strongly_ suggest you to post your next
questions about mod_python there.

regards

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


Re: Checking File permissions

2006-07-20 Thread Avell Diroll
Anoop wrote:
 Please tell me how to check the existence of a file and the read
 permission to the file using python script

You can check the os module (os.stat comes to mind).

For an exemple you can have a look at :
http://www.pixelbeat.org/talks/python/ls.py

Regards,

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


Re: OS specific command in Python

2006-06-21 Thread Avell Diroll
[EMAIL PROTECTED] wrote:
 I have a question on getpass. Since I am a newbie you might find it a
 little dumb.
 
 By using the getpass, are u trying to retrieve the username and
 password of remote mahcine or local ?
 

the module getpass contains 2 functions, getuser() and getpass() :

getuser() returns the username of the user executing the python script 
on the machine where the script is executed.

getpass() prompts the user for a password and returns it in a string 
whitout printing it on screen (just as the text mode login on linux  or 
ssh).

By the way this is better explained in the official python documentation.
http://docs.python.org/lib/module-getpass.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS specific command in Python

2006-06-21 Thread Avell Diroll
3c273 wrote:
 I was just trying to learn how to use .communicate() and all of the examples
 I see have [0] after .communicate(). What is the significance of the [0]?


 From the Python Library Reference 
(http://docs.python.org/lib/node239.html), you learn that the method 
communicate() from the subprocess.Popen() class returns a tuple 
containing the standard output as first item and the standard error of 
the child process as second item. So the [0] in the example is for 
selecting the first item of the tuple ...



from subprocess import *
p1 = Popen([dmesg], stdout=PIPE)
p2 = Popen([grep, hda], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

### is equivalent to :

from subprocess import *
p1 = Popen([dmesg], stdout=PIPE)
p2 = Popen([grep, hda], stdin=p1.stdout, stdout=PIPE)
output_and_error = p2.communicate()
output = output_and_error[0]

### or :

from subprocess import *
p1 = Popen([dmesg], stdout=PIPE)
output=Popen([grep,hda],stdin=p1.stdout,stdout=PIPE).communicate()[0]

### or :

from subprocess import *
p1 = Popen([dmesg], stdout=PIPE)
p2 = Popen([grep, hda], stdin=p1.stdout, stdout=PIPE)
(output, stderror) = p2.communicate()


I hope it was useful ...

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


Re: OS specific command in Python

2006-06-21 Thread Avell Diroll
[EMAIL PROTECTED] wrote:
(snip)
 I have a linux box, from where I remotely execute all the commands. The
 remote machine is windows machine. I installed an OpenSSH server for
 windows to send the shutdown command. I setup the public keys in such a
 way that I could login to SSH server without using password.
 
(snip : script that fits your first need)
 
 I was wondering how can I interact with an application . Since you
 mentioned about subprocess module, I want a ability that my PYthon
 script can actually interact with the windows box and launch and close
 application there remotely.
(snip)

To clear up things a little, you _interact_ with the ssh process 
launched on your local machine from your python script. this ssh process 
_connect_ to an ssh server on your remote winbox. the ssh server then 
execute the requested shell command. So you are limited by what a 
windows shell script can do.
I must warn you that I never used windows much and never thought about 
running a ssh server or even a serious shell session (can you even call 
that a shell ?) on a windows box, so I won't be of much help for the 
_interacting_ with windows process from the windows shell. The only 
suggestion I could make would be to write a python script to be executed 
on the remote machine that would be executed by the ssh server ...

local_machine_script.py

import os
os.system('ssh [EMAIL PROTECTED] python remote_machine_script.py')

###remote_machine_script.py
from subprocess import *
p = Popen(['A_WIN32_COMMAND'], stdout=PIPE)
(output, error) = p.communicate('INPUT_FOR_THE_WIN32_CMD')

This is really a _bad_ way to proceed but it gives you a little more 
interaction.

A better way to interact with the remote shell is to keep the ssh 
session interactive in the first place. To do that os.system is a 'no 
go' and using the subprocess module all the way won't be easy (if even 
possible). For an interactive ssh session you will need some non 
standard library modules. I know 2 modules that would help : pexpect and 
paramiko. Both may help but you will have to choose one or the other.

pexpect
pexpect gives you some control over a child process easing the way to 
pass argument, to get and test the output in order to choose the next 
input to send to the child process.
you can find it here :
http://pexpect.sourceforge.net/
The demo files contain some _great_ examples of how to use it with ssh

paramiko
paramiko implements the ssh2 protocol in python, that way you don't have 
to use a child process to connect to the remote ssh server. After 
establishing the connection you may open an ssh session or whatever you 
need from your local script.
you can find it here :
http://www.lag.net/paramiko/
Again the demo files contain some _great_ examples

I would advise you to download both and to play with the demo files in 
order to choose which one you prefer using.
If you are in a hurry, you should check pexpect first as one of the demo 
files is implementing nearly everything you want.



Hope this helps

-Avell


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


Re: Python at compile - possible to add to PYTHONPATH

2006-06-21 Thread Avell Diroll
rh0dium wrote:
(snip)
 I want to add /foo/bar to the PYTHONPATH build so I don't have to add
 it later on.  Is there a way to do this?
(snip)

If i understand correctly, you want to add a directory to your 
PYTHONPATH for a specific script without modifying the system PYTHONPATH 
global variable ...

To import the Gazonk() class defined in /foo/bar/baz.py without adding 
/foo/bar to PYTHONPATH you can try this:

###

import sys
sys.path.append('/foo/bar')

import baz
quux = baz.Gazonk()

###

hope it helped ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS specific command in Python

2006-06-20 Thread Avell Diroll
[EMAIL PROTECTED] wrote:
 I want to write a python program and call OS specific commands in it.
 So basically, instead of typing in on the command line argument I want
 to have it in a python program and let it do the action.

There are several ways to do so :
* os.system() if you just want to launch a command (kind of fire-and-forget)
* the subprocess module if you want to access stdin, stdout and stderr 
of the launched command

 
 for example. in my program I would want to call the ssh feature like
 one does on the command line
 
 ssh [EMAIL PROTECTED]  .etc

To interact with an ssh process , there is a great module called pexpect 
  (featuring some examples of how to handle ssh process in python)
http://pexpect.sourceforge.net/


OT : paramiko is a module to handle the SSH2 protocol in python
http://www.lag.net/paramiko/

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


Re: OS specific command in Python

2006-06-20 Thread Avell Diroll
[EMAIL PROTECTED] wrote:
 I tried the following and it seemed to work
 
 import os
 os.system('system command here')
 
 Any comments
 

This is an simple way to proceed if you don't need your python script to 
know what happens to the launched process ...
When you need to :
* send some input to the command from the python script after it is launched
* get the output of the command in your python script
* get the pid associated to your command
* wait for your command to finish
* pipe some shell commands
* ...
you should use the subprocess module.

Here is a quick example from the Python Reference Library :
http://docs.python.org/lib/node242.html

##Shell Script :
output=`dmesg | grep hda`


##Python Script :
from subprocess import Popen
p1 = Popen([dmesg], stdout=PIPE)
p2 = Popen([grep, hda], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OS specific command in Python

2006-06-20 Thread Avell Diroll
[EMAIL PROTECTED] wrote:
 When you connect (via ssh or telnet) to a remote machine, you need to
 type (manually)
 your username and your password. Programming that is never easy.
 

This is really eased by the module getpass (std library) :

###

import getpass

login = getpass.getuser()
password = getpass.getpass()

###

If the username is different from your system login this can be changed to :

###

import getpass

login = raw_input('login: ')
password = getpass.getpass()

###


Python definitely comes with batteries included !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GUI and graph

2005-12-22 Thread Avell Diroll
questions? wrote:
 I have a graph with different parameters along different parts of the
 graph.
 
 I want to have a program that can display the graph with coloring for
 different part of the graph. Is this possible in Python? What should I
 read?
 
 Thanks for any comments
 

I would suggest a combination of pyGTK and Matplotlib.
A quick tutorial may be found here : http://www.serpia.org/pygtk

Thinking about it, you might be satisfied with the gnuplot interface for 
python : http://gnuplot-py.sourceforge.net/

Hope that helped ...
-- 
http://mail.python.org/mailman/listinfo/python-list