On 2009-10-29 11:19, Peng Yu wrote:
'symbolic_link' is a symbolic link in the current directory. I run
'python main.py', but it does not return me anything. I want to check
if a file is a symbolic link. I'm wondering what is the correct way to
do so?
$cat main.py
import stat
import os
st =
On Wed, 28 Oct 2009 22:19:55 -0500, Peng Yu wrote:
'symbolic_link' is a symbolic link in the current directory. I run
'python main.py', but it does not return me anything. I want to check if
a file is a symbolic link. I'm wondering what is the correct way to do
so?
$cat main.py
import
import os
if os.path.islink('symbolic_link'):
print hello.
Cheers,
Mahmoud Abdelkader
On Oct 28, 2009, at 11:19 PM, Peng Yu pengyu...@gmail.com wrote:
'symbolic_link' is a symbolic link in the current directory. I run
'python main.py', but it does not return me anything. I want to check
Peng Yu pengyu...@gmail.com writes:
'symbolic_link' is a symbolic link in the current directory. I run
'python main.py', but it does not return me anything. I want to check
if a file is a symbolic link.
You have the same access to the Python help as we do:
import os.path
Matthew Wilson wrote:
I'm using a homemade script to verify some code samples in my
documentation. Here it is:
#! /usr/bin/env python2.6
# vim: set expandtab ts=4 sw=4 filetype=python:
import doctest, os, sys
def main(s):
Run doctest.testfile(s, None)
return
In article zjtsl.34029$zp4.2...@nlpi067.nbdc.sbc.com,
Matthew Wilson m...@tplus1.com wrote:
I'm using a homemade script to verify some code samples in my
documentation. [...]
The script checks all the files listed as arguments. This is OK, but is
there anything better?
Perhaps a
Frank wrote:
Hi,
does anyone know how one can test if, e.g., a dictionary 'name' has a
key called 'name_key'?
This would be possible:
keys_of_names = names.keys()
L = len(keys_of_names)
for i in range(L):
if keys_of_names[i] == name_key:
print 'found'
But certainly not
On Mar 11, 11:49 pm, John Machin [EMAIL PROTECTED] wrote:
On Mar 12, 3:19 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
Paul McGuire [EMAIL PROTECTED] wrote:
will be. For instance, when working with data from 0 to 100 and
looking for frequencies by decade, you can initialize the
On Mar 10, 9:12 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:
if hist.has_key(outcome):
hist[outcome] += 1 # if key already exists, increment
else:
hist[outcome] = 1 # else create a new key
You could write that:
hist[outcome] = 1 + hist.get(outcome, 0)
Or with Python
Paul McGuire [EMAIL PROTECTED] wrote:
will be. For instance, when working with data from 0 to 100 and
looking for frequencies by decade, you can initialize the histogram-
dict with:
for i in range(10):
histodict[i] = 0
A better way, of course (also saving the histodict = {} which you
On Mar 12, 3:19 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
Paul McGuire [EMAIL PROTECTED] wrote:
will be. For instance, when working with data from 0 to 100 and
looking for frequencies by decade, you can initialize the histogram-
dict with:
for i in range(10):
histodict[i] = 0
Frank wrote:
Hi,
does anyone know how one can test if, e.g., a dictionary 'name' has a
key called 'name_key'?
name_key in name
e.g.
name={john: 42}
john in name
True
julie in name
False
--Irmen
--
http://mail.python.org/mailman/listinfo/python-list
Yes, you have name.has_key(name_key) and perhaps better, the in
operator:
if name_key in name:
do something
--
http://mail.python.org/mailman/listinfo/python-list
Sure, you can use if key in dict to test for membership:
Python 2.3.5 (#1, Jan 13 2006, 20:13:11)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type help, copyright, credits or license for more information.
exampledict = {a : 1, b : 2}
a in exampledict
True
q in exampledict
False
Frank wrote:
does anyone know how one can test if, e.g., a dictionary 'name'
has a key called 'name_key'?
Yes. It's already posted; next time have a look at the concise
library reference:
http://docs.python.org/lib/typesmapping.html
This would be possible:
keys_of_names = names.keys()
On Mar 10, 3:17?pm, Frank [EMAIL PROTECTED] wrote:
Hi,
does anyone know how one can test if, e.g., a dictionary 'name' has a
key called 'name_key'?
This would be possible:
keys_of_names = names.keys()
L = len(keys_of_names)
for i in range(L):
if keys_of_names[i] == name_key:
print
if hist.has_key(outcome):
hist[outcome] += 1 # if key already exists, increment
else:
hist[outcome] = 1 # else create a new key
You could write that:
hist[outcome] = 1 + hist.get(outcome, 0)
Or with Python 2.5 you could use
hist = defaultdict(int)
...
hist[outcome]
On 2/22/07, Fabian Steiner [EMAIL PROTECTED] wrote:
Hello!
As the subject says I need to test whether a host computer in our
network is reachable or not. At the moment I simply attempt to connect
to a given port that is open when the machine is online:
[...]
sock =
Fabian Steiner wrote:
Hello!
As the subject says I need to test whether a host computer in our
network is reachable or not. At the moment I simply attempt to connect
to a given port that is open when the machine is online:
[...]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Just because you could ping with ICMP packets doesn't mean you could
do anything with the machine. I assume that you are connecting to
do something on the machine. Just wrap what you are trying to do
in try: block. It will either succeed or fail. Handle the exeption.
And the other way
Hello!
Chris Mellon wrote:
On 2/22/07, Fabian Steiner [EMAIL PROTECTED] wrote:
[...]
Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets. However, I don't know what
On Feb 22, 3:22 pm, Fabian Steiner [EMAIL PROTECTED] wrote:
Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets.
Server or a firewall in between most probably will
Jay Tee [EMAIL PROTECTED] writes:
for j in jobs:
if (j.get('user') == 'jeff' and j.get('state')=='running') :
do_something()
Sounds like you need some backing data structures, like indexes
in a database, e.g. (untested, uses the cool new defaultdicts of 2.5):
index =
On Feb 20, 9:12 am, Paul Rubin http://[EMAIL PROTECTED] wrote:
do_something()
you'd just write:
for j in (index['user']['jeff'] index['state']['running']):
do_something()
Hi,
it looks very cool, except that one of the constraints mentioned is
that the solution has to
Jay Tee [EMAIL PROTECTED] writes:
it looks very cool, except that one of the constraints mentioned is
that the solution has to work properly on pythons 2.2 and 2.3.
That thing doesn't really deeply depend on defaultdict, it's just
convenient. You can add a few more lines of code in the
Hi
your post had the following construct:
for j in (index['user']['jeff'] index['state']['running']):
do_something()
but
Python 2.3.4 (#1, Oct 11 2006, 06:18:43)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type help, copyright, credits or license for more information.
l1=
Jay Tee [EMAIL PROTECTED] writes:
l1= [3, 4, 7, 2]
l2 = [2, 3]
l2 = [2, 3, 99]
l1 l2
Traceback (most recent call last):
File stdin, line 1, in ?
TypeError: unsupported operand type(s) for : 'list' and 'list'
what am I missing?
They are sets, not lists.
from sets import Set as
On Feb 20, 6:44 pm, Paul Rubin http://[EMAIL PROTECTED] wrote:
They are sets, not lists.
from sets import Set as set # use in 2.3 and earlier
l1= set([3, 4, 7, 2])
l2 = set([2, 3])
l2 = set([2, 3, 99])
print l1 l2
Thanks Paul, but:
bosui:~ python
Python 2.2.3 (#1, Oct 26
Jay Tee [EMAIL PROTECTED] writes:
Python 2.2.3 (#1, Oct 26 2003, 11:49:53)
ImportError: No module named sets
Hmm, well I think the sets module in 2.3 is written in Python, so you
could drop it into your application for use in 2.2. Better would be
to use the C version from 2.4 if you can. Or
Hi,
thanks! the code lift from 2.3 to 2.2 worked (thank Guido et al for
BACKWARDS COMPATIBILITY ;-)) ... unfortunately I was in a hurry to get
the release out since a colleague's cluster was croaking under the
load of the old, non-indexed version. Your solution is nicer looking
than mine, and
Jay Tee schrieb:
Hi,
I have some code that does, essentially, the following:
- gather information on tens of thousands of items (in this case, jobs
running on a
compute cluster)
- store the information as a list (one per job) of Job items
(essentially wrapped
dictionaries
Jay Tee wrote:
Hi,
I have some code that does, essentially, the following:
- gather information on tens of thousands of items (in this case, jobs
running on a
compute cluster)
- store the information as a list (one per job) of Job items
(essentially wrapped
dictionaries
On Feb 19, 11:07 am, Peter Otten [EMAIL PROTECTED] wrote:
Use a RDBMS (a database), they tend to be good at this kind of operations.
yeah, one of the options is metakit ... sqlite and buzhug both looked
promising but the constraint of pythons 2.2 and 2.3 ruled that out.
disadvantage of metakit
Jay Tee wrote:
On Feb 19, 11:07 am, Peter Otten [EMAIL PROTECTED] wrote:
Use a RDBMS (a database), they tend to be good at this kind of
operations.
yeah, one of the options is metakit ... sqlite and buzhug both looked
promising but the constraint of pythons 2.2 and 2.3 ruled that out.
Jay Tee [EMAIL PROTECTED] wrote:
Hi,
I have some code that does, essentially, the following:
- gather information on tens of thousands of items (in this case, jobs
running on a
compute cluster)
- store the information as a list (one per job) of Job items
(essentially wrapped
Sandra-24 wrote:
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
I suspect that the and that will work across platforms
parenthesis is in effect a killer. However, if
The current setup will not silently fail when run on win32. How could
it? It doesn't exist; it can't be run.
Ah...didn't know which it did (or didn't do) as I don't have a
win32 box at hand on which to test it.
In chasing the matter further, the OP mentioned that their
particular problem was
It looks like you can get a fairly good apporximation for samefile on
win32. Currently I'm using the algorithm suggested by Tim Chase as it
is good enough for my needs.
But if one wanted to add samefile to the ntpath module, here's the
algorithm I would suggest:
If the passed files do not exist,
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
os.path.samefile(filename1, filename2)
os.path.sameopenfile(fileobject1, fileobject2)
-tkc
--
On Sat, 16 Dec 2006 17:02:04 -0800, Sandra-24 wrote:
Comparing file system paths as strings is very brittle.
Why do you say that? Are you thinking of something like this?
/home//user/somedirectory/../file
/home/user/file
Both point to the same file.
Is there a
better way to test if two
Tim Chase wrote:
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
os.path.samefile(filename1, filename2)
os.path.sameopenfile(fileobject1, fileobject2)
Comparing file system paths as strings is very brittle.
Why do you say that? Are you thinking of something like this?
/home//user/somedirectory/../file
/home/user/file
Or even
~/file
How complicated do you want to get? If you are thinking about aliases,
hard links, shortcuts,
On Sun, 17 Dec 2006 12:30:15 +1100, Steven D'Aprano wrote:
Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
How complicated do you want to get? If you are thinking about aliases,
hard links, shortcuts, SMB shares and
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
os.path.samefile(filename1, filename2)
os.path.sameopenfile(fileobject1, fileobject2)
Nice try, but they
Tim Chase wrote:
[snip]
I'd suggest os.path.samefile which should handle case-sensitive
(non-win32) vs case-insensitive (win32) filenames, soft-links,
and hard-links. Not sure it's prescient enough to know if you
have two remote shares, it will unwind them to their full
server-path name.
On Dec 16, 8:30 pm, Steven D'Aprano
[EMAIL PROTECTED] wrote:
On Sat, 16 Dec 2006 17:02:04 -0800, Sandra-24 wrote:
Comparing file system paths as strings is very brittle.Why do you say that?
Are you thinking of something like this?
/home//user/somedirectory/../file
/home/user/file
Both
Tim Chase wrote:
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
os.path.samefile(filename1, filename2)
os.path.sameopenfile(fileobject1, fileobject2)
Tim Chase wrote:
Comparing file system paths as strings is very brittle.
Why do you say that? Are you thinking of something like this?
/home//user/somedirectory/../file
/home/user/file
Or even
~/file
~ is interpreted as my home directory by the shell, but when it's used
in a
Leif K-Brooks wrote:
~ is interpreted as my home directory by the shell, but when it's used
in a path, it has no special meaning. open('~/foo.txt') tries to open a
file called foo.txt in a subdirectory of the current directory called '~'.
That's what os.path.expanduser is for.
--
Erik Max
Sandra-24 wrote:
Comparing file system paths as strings is very brittle. Is there a
better way to test if two paths point to the same file or directory
(and that will work across platforms?)
No.
There are ways to do it for many operating systems, but there is no
system-independent
Mark Asbach wrote:
Hi pythonians,
I'm one of the maintainers of an open source image processing toolkit
(OpenCV) and responsible for parts of the autotools setup. The package
mainly consists of four shared libraries but is accompanied by a python
package containing some pure python code and
Tim N. van der Leeuw a écrit :
Hi,
I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().
Does something like that exist? (Something which, in case of iterable,
doesn't consume the first element of the iterable)
Bruno Desthuilliers wrote:
Tim N. van der Leeuw a écrit :
Hi,
I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().
Does something like that exist? (Something which, in case of iterable,
doesn't consume
Tim N. van der Leeuw [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Hi,
I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().
How about
try: it = iter(possible_iterable)
except TypeError: bail()
Terry
In [EMAIL PROTECTED], Bruno Desthuilliers wrote:
Tim N. van der Leeuw a écrit :
Hi,
I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().
Does something like that exist? (Something which, in case of iterable,
Marc 'BlackJack' Rintsch a écrit :
In [EMAIL PROTECTED], Bruno Desthuilliers wrote:
Tim N. van der Leeuw a écrit :
Hi,
I'd like to know if there's a way to check if an object is a sequence,
or an iterable. Something like issequence() or isiterable().
Does something like that exist?
Tim,
An object is iterable if it implements the iterator protocol. A good
enough check to see if it does is to check for the presense of the
__iter__() method. The way to do it is:
hasattr(object,'__iter__')
You are correct in the fact that you check if an object is iterable
rather than using
Nick Vatamaniuc [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Tim,
An object is iterable if it implements the iterator protocol
There are presently two iterator protocols. The old one will be likely be
dropped in 3.0 (currently being discussed).
. A good
enough check to see
sillyemperor:
I was a new guy of Python,when i want to test my wxPython app by
unittest,it couldn`t work.I fund a stubmaker.py but it only for
wxDialog but all widgets.Can someone can tell me how test wxPython by
unittest?Thanks
Here's a small example to get you started:
import unittest,
Darren Dale schrieb:
Hello,
I would like to test that latex is installed on a windows, mac or linux
machine. What is the best way to do this? This should work:
if os.system('latex -v'):
print 'please install latex'
but I dont actually want the latex version information to print to
Darren Dale enlightened us with:
I would like to test that latex is installed on a windows, mac or linux
machine. What is the best way to do this? This should work:
if os.system('latex -v'):
print 'please install latex'
The downside is that you can only use this to test by executing.
Dennis Benzinger wrote:
Darren Dale schrieb:
Hello,
I would like to test that latex is installed on a windows, mac or linux
machine. What is the best way to do this? This should work:
if os.system('latex -v'):
print 'please install latex'
but I dont actually want the latex
Sybren Stuvel wrote:
Darren Dale enlightened us with:
I would like to test that latex is installed on a windows, mac or linux
machine. What is the best way to do this? This should work:
if os.system('latex -v'):
print 'please install latex'
The downside is that you can only use this
Darren Dale wrote:
I would like to test that latex is installed on a windows, mac or linux
machine. What is the best way to do this? This should work:
if os.system('latex -v'):
print 'please install latex'
but I dont actually want the latex version information to print to screen. I
[EMAIL PROTECTED] wrote:
Tuples (which are immutable) also appear to be reused
foo = ()
bar = ()
foo is bar
True
Not always:
foo = (1,)
bar = (1,)
foo is bar
= False
--
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL
[EMAIL PROTECTED] a écrit :
If two objects are of equal value you can compare them with ==. What I
want to do is find out if two objects are actually just references to
the same object, how can I do this in Python?
The most obvious way (as usual ?):
if obj1 is obj2:
// your code here
--
Sorry about removing my message, I posted with the wrong google
account, I don't really want my email where those irritating spam bots
can find it.
The most obvious way (as usual ?):
if obj1 is obj2:
// your code here
I immediately thought of is, and tested it in the console, but it
didn't
[EMAIL PROTECTED] writes:
foo = 3
bar = 3
clearly foo and bar have the same value but they are different objects
aren't they?
No, they're the same object. Now try it with 300 instead of 3 ;-).
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED] a écrit :
Sorry about removing my message, I posted with the wrong google
account, I don't really want my email where those irritating spam bots
can find it.
The most obvious way (as usual ?):
if obj1 is obj2:
// your code here
I immediately thought of is, and
Fascinating. With small strings, it uses the same object, and with
small numbers like 3. With 300 they were different objects (why,
shouldn't they both be ints still?)
Mutable objects functioned differently as you suggested:
foo = []
bar = []
foo == bar
True
foo is bar
False
Tuples (which are
On 2005-06-12, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
The most obvious way (as usual ?):
if obj1 is obj2:
// your code here
I immediately thought of is, and tested it in the console, but it
didn't work quite like I expected:
foo = 3
bar = 3
zoo = foo
foo is zoo
True
foo is bar
True
On 2005-06-12, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
Fascinating. With small strings, it uses the same object, and with
small numbers like 3. With 300 they were different objects (why,
It's purely an implimentation detail. The small integers get
used a lot, so Python keeps a pre-created
This isn't a good example to test with, since 3 is an immutable object, as
is 300 and all ints.
It's more meaningful if the objects are mutable. Why do you want to test
identity in the first place?
Roose
[EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Sorry about removing my
Op 2005-01-28, StepH schreef [EMAIL PROTECTED]:
Antoon Pardon a écrit :
Op 2005-01-28, StepH schreef [EMAIL PROTECTED]:
Thanks for you answer.
I'm new to Python (coming from C/C++).
Do you say that it's not possible to test (using unittest) if an exception
is well raised if the tested code
[snipped alot of codes that doesn't mean much ]
if you want to check for exception then it goes like this
try:
put your code here which fails on certain exception like maybe IOError
catch IOError:
put a code to do something when IOError is raised
sorry i won't bother to read your code because
StepH [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
But i've prob with the 1st test : test_badFile.
When I run the test, unittest say that no error is Raised.
But when I run the mps2xml module with a bad file as arg., the exception
is
well Raised.
I assume you don't actually
Thanks for you answer.
I'm new to Python (coming from C/C++).
But i've prob with the 1st test : test_badFile.
When I run the test, unittest say that no error is Raised.
But when I run the mps2xml module with a bad file as arg., the exception
is
well Raised.
I assume you don't actually
StepH [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Do you say that it's not possible to test (using unittest) if an exception
is well raised if the tested code catch it ?
How to solve this paradoxe ? How to automaticaly test such code ?
Then you need a side-effect in catching
Antoon Pardon a écrit :
Op 2005-01-28, StepH schreef [EMAIL PROTECTED]:
Thanks for you answer.
I'm new to Python (coming from C/C++).
Do you say that it's not possible to test (using unittest) if an exception
is well raised if the tested code catch it ?
How to solve this paradoxe ? How to
StepH [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
So why the assertRaises function in unittest ? My goal is to test if an
exception is well raised when a bad filename is passed to the mps2xml
function.
It is for functions in which the exception is raised and not caught. You
201 - 280 of 280 matches
Mail list logo