Re: Temporary but named file with BSDDB

2010-06-13 Thread Tim Pinkawa
On Sun, Jun 13, 2010 at 11:01 PM, Jason  wrote:
> I'd like to use the BSDDB module in an app (intended only for GNU/
> Linux like OSes).
>
> I don't need the on-disk file to hang around after I've used it. So as
> per the docs I gave it "None" for the filename.
>
> The problem is, it creates the temporary file in /var/tmp. If the user
> uses separate root and home partitions, the root partition is usually
> significantly smaller, and the file created will fill up the partition
> pretty quickly. Besides which, I'd rather have the file placed in the
> app's own working area under the user's home dir.
>
> I'm aware that I can pass in a path of my choosing to use for the DB.
> But then I'm at the other extreme of resource management — I need to
> clean it up myself, making sure there's no path out of the code that
> results in the file getting left behind, including exceptions or
> signals.
>
> Is there an in-between way to use the BSDDB module, where I can name
> the file I want to use but tell the module it's temporary so that it
> can be removed without my intervention?
>
> - Jason

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

The tempfile.TemporaryFile class should do the job. This part is
probably of particular interest to you:

"It will be destroyed as soon as it is closed (including an implicit
close when the object is garbage collected)."

Short of the interpreter itself crashing (exceedingly rare in my
experience), the file will be deleted regardless if your program exits
normally or as the result of an exception.

As for the location of the file:

"If dir is specified, the file will be created in that directory;
otherwise, a default directory is used. The default directory is
chosen from a platform-dependent list, but the user of the application
can control the directory location by setting the TMPDIR, TEMP or TMP
environment variables."

My guess is it would end up in /tmp by default, unless your system is
configured otherwise.

Hope this helps,
Tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE - Parsing ipconfig /all output - question

2010-06-06 Thread Tim Pinkawa
On Sun, Jun 6, 2010 at 10:47 PM, joblack  wrote:
> I'm trying to get the first MAC address from the ipconfig /all output.
> Unfortunately you can't just search for Physical Address because the
> name is only valid in the English Windows version.

> Any ideas?

(accidentally sent original to Johannes only)

This filters out all the false positives on my machine (Windows 7 x64 English):

import subprocess
import re
p = subprocess.Popen('ipconfig /all', shell = True, stdout=subprocess.PIPE)
p.wait()
rawtxt = p.stdout.read()
print rawtxt

p = re.findall(r'\s([0-9A-F-]{17})\s',rawtxt)
print p

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


Re: python library call equivalent to `which' command

2009-06-29 Thread Tim Pinkawa
On Mon, Jun 29, 2009 at 2:31 PM, Tim Pinkawa wrote:
> I am curious about it being slow, though. Is there a faster way to get
> the contents of a directory than os.listdir() or is there a faster way
> to see if an element is in a list other than "x in y"? I believe
> 'which' will terminate once it finds any match, which mine does not,
> but that can be fixed by adding a break after the print.

To answer my own question on this specific case, you could check with
os.access which may be faster. I am still curious, Christian, if your
issue with it being slow was that "os.listdir is slow" or "os.listdir
is slow in this case".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python library call equivalent to `which' command

2009-06-29 Thread Tim Pinkawa
On Mon, Jun 29, 2009 at 2:17 PM, Christian Heimes wrote:
> "if file in os.list()" is slow and not correct. You have to check if the
> file is either a real file or a symlink to a file and not a directory or
> special. Then you have to verify that the file has the executable bit, too.

I realize four lines of Python does not replicate the functionality of
which exactly. It was intended to give the original poster something
to start with.

I am curious about it being slow, though. Is there a faster way to get
the contents of a directory than os.listdir() or is there a faster way
to see if an element is in a list other than "x in y"? I believe
'which' will terminate once it finds any match, which mine does not,
but that can be fixed by adding a break after the print.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python library call equivalent to `which' command

2009-06-29 Thread Tim Pinkawa
On Mon, Jun 29, 2009 at 12:54 PM, destroy wrote:
> Hi,
>  I'm looking for a Python library function that provides the same
> functionality as the `which' command--namely, search the $PATH
> variable for a given string and see if it exists anywhere within. I
> currently examine the output from `which' itself, but I would like
> something more portable. I looked through the `os' and `os.path'
> modules but I didn't find anything.

This works on POSIX systems. Windows uses semicolons to separate paths
rather than colons so that would need to be taken into account when
running on Windows. This also doesn't recognize shell built-ins, only
real binaries.

import os

def which(file):
for path in os.environ["PATH"].split(":"):
if file in os.listdir(path):
print "%s/%s" % (path, file)

>>> which("ls")
/bin/ls
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to extract some text?

2009-03-08 Thread Tim Pinkawa
On Sun, Mar 8, 2009 at 5:18 PM, Oltmans  wrote:
> I'm at a loss to figure out how to extract some text from a string.
> Here is a string:
>
> setTimeout("location.href='http://youtube.example.com/login.aspx'",
> 5000);
>
> and I want to only retrieve the URL from above i.e I only want this
> http://youtube.example.com/login.aspx from the above string. Any ideas/
> help is highly appreciated.
>
> Thanks,
> Oltmans

If x is your string:
>>> pos = x.find("'") + 1
>>> x[pos:x.find("'", pos)]
'http://youtube.example.com/login.aspx'

Find the first single quote, then get the character range between that
and the next single quote.

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