Re: I just wrote my first Python program a guessing game and it exits with an error I get this.

2013-06-05 Thread Tim Golden
On 05/06/2013 16:14, Armando Montes De Oca wrote:
> On Wednesday, June 5, 2013 10:40:52 AM UTC-4, Armando Montes De Oca wrote:
>> Traceback (most recent call last):
>>
>>   File "Guessing_Game.py", line 32, in 
>>
>> input (enter)
>>
>>   File "", line 0
>>
>> ^
>>
>> SyntaxError: unexpected EOF while parsing


Armando. Try this at a Python prompt, and just press Enter without
entering any text.

  input("Please enter something:")


The trouble is that the beguilingly-named "input" function actually
*evaluates* what you type, ie it's the same as doing this:

  eval("")

which, as you can see, gives the same error message. You're clearly
using Python 2.x as your prints are statements, so input has this
characteristic. Instead you should use raw_input:

  raw_input(enter)

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


DOS or not? [was Re: How to tell Script to use pythonw.exe ?]

2013-07-03 Thread Tim Golden
On 03/07/2013 02:34, Andrew Berg wrote:
> DOS is long
> dead, and is much, much different under the hood from the console
> subsystem in modern versions of Windows.


While this is clearly true, it's by no means unusual for people to refer
to the "DOS Box" or talk about "DOS commands" etc. even when they're
quite well aware of the history of Windows and its Console subsystem.
It's just quicker than saying "Console Window" or something.

I mention this because it seems to get called out every time someone
uses the term "DOS" on this and other Python lists and it can smack
slightly of pedantry.

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


Re: DOS or not? [was Re: How to tell Script to use pythonw.exe ?]

2013-07-03 Thread Tim Golden
On 03/07/2013 09:28, Andrew Berg wrote:
> On 2013.07.03 02:34, Tim Golden wrote:
>> While this is clearly true, it's by no means unusual for people to
>> refer to the "DOS Box" or talk about "DOS commands" etc. even when
>> they're quite well aware of the history of Windows and its Console
>> subsystem. It's just quicker than saying "Console Window" or
>> something.
>> 
>> I mention this because it seems to get called out every time
>> someone uses the term "DOS" on this and other Python lists and it
>> can smack slightly of pedantry.

> I really would like to prevent those who are not very familiar with
> Windows or its history from associating the limits and internal
> behavior of MS-DOS with the console subsystem of Windows NT.
> Especially with the anti-Windows sentiment that seems to be getting
> more popular (not that it's entirely without merit, but that's
> another topic for another day on another list), I'd rather see 
> operating systems judged on things that are actually true and not
> miscellaneous FUD spread by ignorance and confusion. 

We can certainly agree on this. I can't count the number of emails I've
deleted as too hot-headed in response to dismissive comments about
Windows as a platform. Some of them, at least, appear to be from people
who last actually used Windows back in the 9x days when the command
window was very limited indeed.

I realize it can
> come off as pedantic, but what may be obvious to those with of us
> with a lot of experience with different operating systems over the
> years can easily slip past a novice.

I suppose I view it in the same light as people (very few, thankfully,
in my experience) who go out of their way to correct "MB" to "MiB" when
talking about disk sizes. Or -- and I'm definitely guilty of this -- of
pointing out that London telephone numbers are all 020 plus eight digits
and *not* 0207 or 0208 plus seven digits. Whenever I do that I'm aware
that I'm technically in the right but that, for all practical purposes,
it's a needless precision.

Obviously, if it were clearly a source of confusion in some context I'd
clarify what needed to be clarified.


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


Re: DOS or not? [was Re: How to tell Script to use pythonw.exe ?]

2013-07-03 Thread Tim Golden
On 03/07/2013 13:50, Tim Chase wrote:
> On 2013-07-03 09:51, Tim Golden wrote:
>> We can certainly agree on this. I can't count the number of emails
>> I've deleted as too hot-headed in response to dismissive comments
>> about Windows as a platform. Some of them, at least, appear to be
>> from people who last actually used Windows back in the 9x days when
>> the command window was very limited indeed.
> 
> I guess one of my biggest frustrations with the cmd.exe (and
> command.com) interpreters is that argument processing is left to the
> application, so each application may do it slightly differently:

Goodness, I doubt if you'll find anyone who can seriously make a case
that the Windows command prompt is all it might be. I'm not a Powershell
user myself but people speak highly of it. Or, as you say, you can use
the GNU tools either natively or via cygwin. Not my cup of tea, but
that's the way of tools: one man's meat...

More to the point, I've got no problem with informed criticism (although
there's little point in grumbling just for the sake of it). The problem
I have is with criticisms which are years out of date, or which appear
to be fuelled by prejudice more than by experience.

TJG

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


Re: python.exe crash if opencv tries to access busy webcam

2013-07-03 Thread Tim Golden
On 03/07/2013 14:25, [email protected] wrote:
> Hello,
> 
> 
> I have a while loop taking images every 5 minutes from webcam.
> Unfortunately, if the camera is busy, python.exe crashes and there is
> no exception to catch. Is there a way to check if camera is busy to
> avoid the crash?

If python.exe crashes -- ie exits completely without a traceback, then
it's most likely a flaw in the extension module which is capturing the
image. If that's the case, there's nothing you can do in Python code to
prevent it; you need to work with the project maintainers to fix that.

If you're getting a traceback but the console window is then closing too
fast for you to see it, then try running the code from an open console
window rather than double-clicking on it. (You don't say how you're
kicking the code off so I'm offering this just in case)

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


Re: Python Script Hashplings

2013-07-26 Thread Tim Golden
On 26/07/2013 11:37, Devyn Collier Johnson wrote:
> 
> On 07/25/2013 09:54 AM, MRAB wrote:
>> On 25/07/2013 14:42, Devyn Collier Johnson wrote:
>>> If I execute a Python3 script with this haspling (#!/usr/bin/python3.3)
>>> and Python3.3 is not installed, but Python3.2 is installed, would the
>>> script still work? Would it fall back to Python3.2?
>>>
>> Why don't you try it?
>>
>>> I hope Dihedral is listening. I would like to see another response
>>> from HIM.
>>>
>>
> Good point, but if it falls back to Python3.2, how would I know? Plus, I
> have Python3.3, 3.2, and 2.7 installed. I cannot uninstall them due to
> dependencies.

Devyn, I'm not a *nix person so someone can point out if I'm wrong, but
my understanding is that the shebang line (or whatever you want to call
it) just tells the shell: run this command to run this file. So you can
put "#!/usr/bin/fish-and-chips" as the first line and it will try to run
the file using /usr/bin/fish-and-chips.

If you put #!/usr/bin/python3.3 the shell will use the executable
/usr/bin/python3.3. It doesn't know or care about Python or its
versions: it won't go looking for some alternative binary. If
/usr/bin/python3.3 isn't there, the shell will fail to run the code with
some kind of error message.

If you or your package manager symlink /usr/bin/python3 to whatever the
latest Python 3.x is on your system then you can safely use
/usr/bin/python3 throughout and let the symlink do the work!

TJG

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


Re: C extension module doesn't throw exception after setting error indicator through PyErr_SetString()

2012-08-02 Thread Tim Golden
On 02/08/2012 09:57, rahul wrote:
> I am implementing a C extension module, during this I saw that when I
> set the global error indicator and error message through
> PyErr_SetString() API and return false.
> 
> But it doesn't throw any error when I tried to check the error
> through sys.exc_info() then it returns (NULL, NULL, NULL) tuple.
> 
> When I return NULL through the C extension module's function then it
> works correctly and throws the exception as expected.
> 
> The skeleton looks like:
> 
> static PyObject* check(PyObject* sef, PyObject* args) { PyObject*
> input = NULL; if (!PyArg_ParseTuple(args, "O", &input)){ return
> NULL; } . . PyErr_SetString(PyExc_Exception, "Throwing Error
> through check function"); Py_RETURN_FALSE; }
> 
> Any idea on why this is happening? Any help will be appreciated.


Because you're returning False. You should be returning NULL. Which is
why it works when you do :)

Have a look at the first line of this page:

http://docs.python.org/py3k/extending/extending.html#intermezzo-errors-and-exceptions


Which piece of documentation led you to think that returning False was
the thing to do here? Perhaps there's a doc that needs fixing?


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


Re: C extension module doesn't throw exception after setting error indicator through PyErr_SetString()

2012-08-02 Thread Tim Golden
On 02/08/2012 10:21, rahul wrote:
> 
> Hi TJG,
> 
> The above link also doesn't strictly said that return value should be
> NULL only, it only said that usually NULL pointer used. No where I
> saw that it is nessasory t
> 
> At http://docs.python.org/c-api/exceptions.html. it is written that
> "Most functions also return an error indicator, usually NULL if they
> are supposed to return a pointer, or -1 if they return an integer
> (exception: the PyArg_*() functions return 1 for success and 0 for
> failure)." this also told that usually NULL is used but we can change
> the return error indicator to any value. As like PyArg_*() used 0 for
> error value.

The docs you quote are very slightly confusing because they're combining
the standard practice (return NULL), the uncommon alternative (return
-1) and a few special cases present in the core functions.

In short, any function you expose in an extension module will be
returning a PyObject* and should return NULL if it is setting or
cascading an exception.

I'm not convinced that the docs need altering here: this is the first
time I've come across anyone who was confused. But if you think some
different wording might help, feel free to propose something.

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


Re: C extension module doesn't throw exception after setting error indicator through PyErr_SetString()

2012-08-02 Thread Tim Golden
On 02/08/2012 10:50, rahul wrote:
> When I use same code base for Python 3.x, then behavior is different. In this 
> when I return false then also it throws exception but only when any other 
> statement get executed after this 
> 
> like below code:
>  ...
>  ...
>b = None
>try:
>  a = testModule.check(None)
>except:
>  b = sys.exc_info()
> then code execution doesn't come to except block.
> But when I add one statement after calling check function then code execution 
> goes into except block. 
> 
>  ...
>  ...
>  b = None
>  try:
>a = testModule.check(None)
>print( a )
>  except:
>b = sys.exc_info()


Sounds like you're entering into undefined behaviour. If you set an
exception and don't return NULL, you're leaving Python's internals in an
inconsistent state. I don't know the internal code paths, but presumably
in one version it just ignored the exception state while in the other it
noticed it but in a different point in the code at which point it raised
the exception. Or something.

Long-and-short: return NULL from an extension module function once
you've raised (or are cascading) an exception condition. Anything else
is undefined unless you know *exactly* what you're doing.


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


Re: Running Python web apps on shared ASO servers?

2012-08-12 Thread Tim Golden

On 12/08/2012 21:52, Gilles wrote:

On Sun, 12 Aug 2012 07:56:26 +0200, Dieter Maurer
 wrote:

You should probably read the mentioned forum resources to learn
details about the Python support provided by your web site hoster.


Yup, but so far, no answer, so I figured someone here might now.

Those articles seem to indicate that CGI isn't a good solution when
mod_python isn't available


Just to make a point: one person's "isn't a good solution" is another 
person's "works perfectly well for me". Modern servers are really quite 
quick: the cost of starting up a Python process and generating an HTML 
page can be really quite low. I've certainly had low-traffic production 
websites running for years on CGI without anyone complaining.


If speed was an issue or if I thought that I'd be getting more requests 
than I am then I'd consider a more sophisticated solution.


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


Re: Filter versus comprehension (was Re: something about split()???)

2012-08-25 Thread Tim Golden

On 25/08/2012 13:57, David Robinow wrote:

On Fri, Aug 24, 2012 at 3:03 PM, Walter Hurry  wrote:

On Fri, 24 Aug 2012 14:29:00 -0400, Dennis Lee Bieber wrote:


It appears to be a change Google made in the last month or two... My
hypothesis is that they are replacing hard EOL found in inbound NNTP
with an HTML , and then on outgoing replacing the  with a pair of
NNTP line endings. In contrast, text composed on Google is coming in as
long single lines (since quoting said text in a response produces on a
">" at the start of the paragraph.


Google Groups sucks. These are computer literate people here. Why don't
they just use a proper newsreader?

I haven't used a newsreader in over a decade. I'm quite happy with a
mailing list. Am I missing something?


Not really. I'm the same; it just means you can skip over the occasional 
ggroups-newsreader discussion threads which pop up

about 3 times a year on average.

:)

TJG

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


Re: how to get os.py to use an ./ntpath.py instead of Lib/ntpath.py

2012-09-11 Thread Tim Golden
On 11/09/2012 04:46, Steven D'Aprano wrote:
> On Mon, 10 Sep 2012 15:22:05 -0700, ruck wrote:
> 
>> On Monday, September 10, 2012 1:16:13 PM UTC-7, Steven D'Aprano wrote:
> [...]
>>> That's not so much a workaround as the officially supported API for
>>> dealing with the situation you are in. Why don't you just prepend a 
>>> '?' to paths like they tell you to?
>>
>> Good idea, but the first thing os.walk() does is a listdir(), and
>> os.listdir() does not like the r'\\?\' prefix.  In other words,
>> os.walk(r'\\?\C:Users\john\Desktop\sandbox\goo') does not work.
> 
> Now that sounds like a bug to me. If Microsoft officially support 
> leading ? in file names, then so should Python on Windows.

And so it does, but you'll notice from the MSDN docs that the \\?
syntax must be supplied as a Unicode string, which os.listdir
will do if you pass it a Python unicode object and not otherwise:

import os
os.listdir(u"?\\c:\\users")

# and consequently

for p, ds, fs in os.walk(u"?\\c:\\users"):
  print p


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


Re: Python on Windows

2012-10-19 Thread Tim Golden
On 19/10/2012 14:24, graham wrote:
> Thanks to everyone who replied.
> 
> Python was installed in the subdirectory C:\Python27 with the file
> feedparser.py residing in C:\Python27\Lib\email.
> 
> Setting the Windows environment variable (which did not previously
> exist) to C:\Python27\Lib\email allowed me to import feedparser
> successfully.
> 
> However, it seems that this feedparser module is not the module I wanted.
> 
> I'm trying to follow an introductory Python course from the magazine
> Linux Format (issue number 120 I think). 


I'm very surprised that the article tells you to import a non-standard
module without telling you where to download it. I imagine that the
module is this one:

  http://pypi.python.org/pypi/feedparser/

(In general, PyPI is the first place to look for Python packages).

> This is all confusing and frustrating.

Understandably. It's not unknown, but it is unusual for two
identically-named packages to exist for Python. It's more
unfortunate when it's one used in a beginner's article.

If you understand how to download the .zip file from the page above and
unzip it then you need to go to the directory where the unzipped files
are and run:

  python setup.py install

If you're not sure how to do any of that, feel free to post back here or
to the python-tutor list [1] which is a little more experience in
helping newcomers.

TJG

[1] http://mail.python.org/mailman/listinfo/tutor


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


Re: Python on Windows

2012-10-19 Thread Tim Golden
On 19/10/2012 14:24, graham wrote:
> Python was installed in the subdirectory C:\Python27 with the file
> feedparser.py residing in C:\Python27\Lib\email.
> 
> Setting the Windows environment variable (which did not previously
> exist) to C:\Python27\Lib\email allowed me to import feedparser
> successfully.

As an aside, this is not the best use of the PYTHONPATH
variable. (I would argue that, on Windows at least, there's very little
need for the env var).

In general, you'll want to be using a mechanism such as pip:

  http://pypi.python.org/pypi/pip

which will look things up on PyPI so you can just do "pip install
newmodule". This will install into c:\python27\lib\site-packages. The
same for a module you install "manually" (ie python setup.py install) or
via an .exe or an .msi from PyPI or elsewhere.

There are other possibilities: .pth files, the PYTHONPATH env var,
virtualenv, user-install directories which you can read about, but in
general, just let the standard mechanisms install into
c:\python27\lib\site-packages (which is automatically on sys.path) and
go from there.

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


Re: Python on Windows

2012-10-19 Thread Tim Golden
[Could I suggest snipping some of the preceding replies unless you're
referring directly to them? Just leave enough to make the context clear]

[... attempts to find feedparser module for beginner's tutorial ...]

On 19/10/2012 15:12, graham wrote:
> Once again thanks to those that replied.
> 
> Since I posted, I found a version 4.1 on sourceforge.
> 
> I guessed/muddled my way through installation and it seems to work.
> 
> To Tom Golden,
>   Thanks for the link to http://pypi.python.org/pypi/feedparser/ - I
> couldn't find it and I don't know how you did.

At least from where I'm standing, it's the second Google result for
"python feedparser" but in fact I Googled for "PyPI feedparser" because
I knew that that was the most likely bet.

> 
> The version at http://pypi.python.org/pypi/feedparser/ is 5.1. Can I
> simply install 'over the top' of the previous one or do I need to
> uninstall V4.1?

You should just be able to over-install. No harm in deleting the
previous one, but you probably don't need to.

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


Re: Python on Windows

2012-10-19 Thread Tim Golden
On 19/10/2012 15:23, Mark Lawrence wrote:
> On 19/10/2012 14:44, Tim Golden wrote:
>>
>> (In general, PyPI is the first place to look for Python packages).
>>
>>
> 
> For the benefit of the OP and others this is worth reading on how to get
> Python packages from pypi that let you get Python packages from pypi
> http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows :)
> 

Heh. I avoided trying to explain how to install pip because the OP
seemed unfamiliar with enough concepts already. It never occurred to me
to use easy_install to install pip!

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-22 Thread Tim Golden
On 22/10/2012 15:51, Kevin Holleran wrote:
> Back at it this morning.  The RPC was due to needing to run it under
> another account (or so I think now...). However, the RemoteRegistry
> service is not just STOPPED but DISABLED.
> 
> I am trying to see if there is a call to actually set the state to
> MANUAL.  Then I can star the registry, grab what I need, stop the
> service, then set it back to disabled
> 
> Does anyone know if there is a way to do this?  

Can you connect to the remote machine via WMI? (If the remote registry
service is stopped, WMI might be also). If so, you can access the
registry remotely via WMI:


  http://timgolden.me.uk/python/wmi/cookbook.html#list-registry-keys

Ultimately, you need *something* on the remote machine to be running
which will accept incoming requests. If nothing is (because the machine
& network are secured) then you're not going to be able to do what you want.

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-22 Thread Tim Golden

On 22/10/2012 16:38, Kevin Holleran wrote:

Thanks, I will look into that.  WMI is enabled, but everything WMI query I
wrote (& I am NOT a WMI expert or even close) gave me a bunch of NIC
info, but not the info I am after in the registry (driver description,
driver date, driver version for the NICs).


I assume you've found things like the Win32_NetworkAdapter which doesn't 
include driver details. If you cared to come across with the registry 
keys / values you needed I'm sure I could rustle up a sample query to 
get you on the right path.


TJG

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-23 Thread Tim Golden
On 22/10/2012 21:01, Kevin Holleran wrote:
> Tim,
> 
> I am looking here:
> 
> SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{BF9F6FB0-C999-4D19-BED0-144F77E2A9D6}
> 
> Enumerating the keys for a BusType == 5, then grabbing the values of
> DriverDesc, DriverDate, & DriverVersion.
> 
> So I am doing this:

[... snip querying uninstallers ...]

I don't have that particular uninstaller key but the code below, using
the wmi module to hide the plumbing, queries all the installers and
should give you enough of an idea, hopefully. For brevilty, I've only
bothered with extracting string values; it would be easy to extract
other datatypes.

To perform the same query on another computer, just pass the other
computer name (or IP address) as the first parameter to the wmi.WMI call
(or use the named param "computer").


import _winreg as winreg
import wmi

HKLM = winreg.HKEY_LOCAL_MACHINE
UNINSTALLERS = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

registry = wmi.WMI(namespace="default").StdRegProv
_, names = registry.EnumKey(HKLM, UNINSTALLERS)
for name in names:
print name
uninstaller = UNINSTALLERS + "\\" + name
_, value_names, value_types = registry.EnumValues(HKLM, uninstaller)
for value_name, value_type in zip(value_names, value_types):
if value_type == winreg.REG_SZ:
_, value = registry.GetStringValue(
  HKLM, uninstaller, value_name
)
else:
value = "(Non-string value)"
print u"  ", value_name, u"=>", value




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


Re: Fast forward-backward (write-read)

2012-10-24 Thread Tim Golden
On 24/10/2012 08:07, Virgil Stokes wrote:
> On 23-Oct-2012 22:03, Cousin Stanley wrote:
>> Virgil Stokes wrote:
>>
>>> Not sure about "tac" --- could you provide more details on this
>>> and/or a simple example of how it could be used for fast reversed
>>> "reading" of a data file ?
>>tac is available as a command under linux 
>>
>>$ whatis tac
>>tac (1) - concatenate and print files in reverse
>>
>>$ whereis tac
>>tac: /usr/bin/tac /usr/bin/X11/tac /usr/share/man/man1/tac.1.gz
>>
>>$ man tac
>>
>>SYNOPSIS
>>  tac [OPTION]... [FILE]...
>>
>>DESCRIPTION
>>
>>  Write each FILE to standard output, last line first.
>>
>>  With no FILE, or when FILE is -, read standard input.
>>
>>
>>I only know that the  tac  command exists
>>but have never used it myself 
>>
>>
> Unfortunately, I may be forced to process the data on a Windows
> platform; but, thanks Cousin for the Linux tip.

Well, addressing that specific point, tac is available for Windows:

  http://unxutils.sourceforge.net/

No idea how efficient it is...

TJG

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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Tim Golden
On 24/10/2012 12:40, Kevin Holleran wrote:> Here is the full traceback:
>
> [output]
> Scan_NIC_Driver_Info_1.2.py  -i
> testing_MSK_Server.csv -o MSK_Test_output.csv -u unreachable.csv
> Parsing input file...
>
> Connecting to IP...
>
> Traceback (most recent call last):
>   File
> "D:\Development\Scripts\Python\Scan_NIC_Driver_Info\Scan_NIC_Driver_Info_
> 1.2.py ", line 70, in 
> _, names = registry.EnumKey(HKLM,NICs)
>   File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
> handle_com_error ()
>   File "C:\Python27\lib\site-packages\wmi.py", line 241, in
handle_com_error
> raise klass (com_error=err)
> wmi.x_wmi:  occurred.', (0,
>  u'SWbemProperty', u'Type mismatch ', None, 0, -2147217403), None)>
>
> [/output]
>
> Basically, it parses a list of hosts from a CSV, outputs a series of
> lines: host, driverDesc, diverDate, driverVersion.  Basically it pings
> the host first to make sure its up.
>
> Here are the relevant parts to the script:
>
> [code]
>  registry = wmi.WMI(host, namespace="default").StdRegProv
> _, names = registry.EnumKey(HKLM,NICs)

Could you confirm what version of Windows is running on the remote
machine? Also, could you show the output of the following, please:


import wmi

host = "" # pick one

print wmi.WMI(host, namespace="default").StdRegProv




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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Tim Golden
On 24/10/2012 13:36, Kevin Holleran wrote:
> Here is the output as you requested.  Again thanks for your time & help.
>  I hate monopolizing one person's time so much

Heh. Everyone else is welcome to chip in :)

Ok, try specifying the parameter names. (I remember someone having
problems before caused by mismatched param order):


import wmi
import _winreg as winreg

host = "..."
HKLM = winreg.HKEY_LOCAL_MACHINE
NICs =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"

registry = wmi.WMI(host, namespace="default").StdRegProv
registry.EnumKey(hDefKey=HKLM, sSubKeyName=NICs)



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


Re: Is there a way to programmatically turn on remote registry?

2012-10-24 Thread Tim Golden
On 24/10/2012 14:26, Kevin Holleran wrote:
> On Wed, Oct 24, 2012 at 9:11 AM, Tim Golden  Ok, try specifying the parameter names. (I remember someone having
> problems before caused by mismatched param order):
> 
> 
> 
> OK, tried that as well as specifying the parameters directly instead of
> through variables, with the same result  I even looked back through
> my old scripts but I have not used the WMI module in this way... only to
> do WMI queries...

Couple of things:

Does the same thing happen if you run against your own machine?

What's the result of the code below (which is basically "raw" WMI,
without the wmi module):


import _winreg as winreg

import win32com.client

HKLM = winreg.HKEY_LOCAL_MACHINE
NICs =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}"

registry = win32com.client.GetObject("winmgmts:/root/default:StdRegProv")
EnumKey = registry.Methods_("EnumKey")
params = EnumKey.InParameters.SpawnInstance_()
params.Properties_.Item("hDefKey").Value = HKLM
params.Properties_.Item("sSubKeyName").Value = NICs

OutParameters = registry.ExecMethod_("EnumKey", params)
print OutParameters.Properties_.Item("ReturnValue")
print OutParameters.Properties_.Item("sNames")




TJG

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


Re: Question about long-running web scripts

2012-10-25 Thread Tim Golden
On 25/10/2012 12:45, Gilles wrote:
> I'd like to check something about running Python web applications.
> 
> Generally speaking, the reason scripts run faster when called
> through FastCGI or the mod_* modules, is because the interpreter is
> already up and running. But when running PHP scripts, this does
> nothing about fetching the file from disk, recompiling, rerunning it,
> and usually reconnecting to the database.
> 
> OTOH, Python web scripts can be written as long-running scripts: In 
> this case, what is the added-value of using FastCGI? Why can't the
> web server simply call the Python script directly, just like CGI?

(Your question is a little confused at the end. I'm choosing to
understand: why can't we just run Python one-shot, like CGI? The likely
alternative meaning is: why can't the incoming request be routed to an
already-running Python program -- which is not, of course, what CGI
generally does. Hence my confusion).

The answer is: it can. CGI is a protocol rather than anything else. In
front of a CGI exchange is the browser (or some other web client).
Behind it is some program which is capable of producing a valid HTTP
response, including a Python program.

It's perfectly possible to run a usable website against Python running
one-shot. You won't get terrific performance out of it, but for a
website which doesn't expect humungous amounts of traffic, it'll work fine.

The amount of time it takes a half-decent, even shared, server to start
up a Python process, connect to a database, pull stuff together, and
send a response will likely not impact on an average user's experience.
As long as too many of them don't try to do that at the same time.
Exactly where the line is drawn will depend on your particular hosting
solution, your assumed traffic, and your users' expectations as to
responsiveness.

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


Re: Question about long-running web scripts

2012-10-25 Thread Tim Golden
On 25/10/2012 13:40, Gilles wrote:
> On Thu, 25 Oct 2012 13:03:14 +0100, Tim Golden 
> wrote:
>> (Your question is a little confused at the end. I'm choosing to
>> understand: why can't we just run Python one-shot, like CGI? The likely
>> alternative meaning is: why can't the incoming request be routed to an
>> already-running Python program -- which is not, of course, what CGI
>> generally does. Hence my confusion).
> 
> Yes indeed. Sorry about the confusion.
> 
> But actually, I didn't mean one-shot scripts, where the Python
> interpreter + script must be loaded each time, but rather: If I leave
> a Python running in an endless loop, why not just use either CGI or
> some other basic way to call the script instead of FastCGI?

In essence, you're describing FastCGI. A Python program (or, indeed, any
program) which uses FastCGI runs continuously and waits for the incoming
request on a TCP socket (instead of as a sys.stdin stream + env vars
immediately after process startup).

The key description is here:

  http://www.fastcgi.com/drupal/node/6?q=node/15

(The sections have no anchors; you're looking for the section titled "2.
FastCGI Interface")

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


Re: Question about long-running web scripts

2012-10-26 Thread Tim Golden
On 26/10/2012 10:58, Gilles wrote:
> On Thu, 25 Oct 2012 14:24:16 +0100, Tim Golden 
> wrote:
>>> But actually, I didn't mean one-shot scripts, where the Python
>>> interpreter + script must be loaded each time, but rather: If I leave
>>> a Python running in an endless loop, why not just use either CGI or
>>> some other basic way to call the script instead of FastCGI?
>>
>> In essence, you're describing FastCGI. A Python program (or, indeed, any
>> program) which uses FastCGI runs continuously and waits for the incoming
>> request on a TCP socket (instead of as a sys.stdin stream + env vars
>> immediately after process startup).
> 
> Thanks for the clarification.
> 
> Since, unlike PHP, the Python interpreter is not available in a
> FastCGI-capable version, this explains why the www server must be told
> which specific Python script to run through FastCGI.

I think that this is the distinction you're making:

PHP: mod_php (fastcgi mode) runs myscript.php

Python: .py runs myscript.py

which is, essentially, true, not least because PHP and web apps are
pretty much synonymous in many people's minds. Both ways: the only thing
PHP does is web; the simplest route to a web app is PHP.

Certainly there are Python equivalents (mod_python, mod_wsgi, etc.)
which can run in effectively the same way as mod_php, and they could be
configured to run an fcgi frontend script, I presume. There's always a
certain confusion here because you can often one mechanisms (say,
mod_wsgi) to act as another (say legacy one-shot CGI) and because some
things are both mechanism and protocol and it's not always easy to tease
the two apart.


> 
> The reason I ask for all this, is that I want to understand how things
> work under the hood before relying on a Python framework to handle the
> nitty-gritty.

Good scheme.

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


Re: Proper place for everything

2012-11-02 Thread Tim Golden
On 02/11/2012 11:20, Jason Benjamin wrote:
> Anybody know of the appropriate place to troll and flame about various
> Python related issues?  I'm kind of mad about some Python stuff and I
> need a place to vent where people may or may not listen, but at at least
> respond.  Thought this would be a strange question, but I might as well
> start somewhere.

A Blog seems the obvious choice. If you don't already have one there are
certainly freely-hosted ones available: blogspot, wordpress, etc.

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


Re: Proper place for everything

2012-11-02 Thread Tim Golden
On 02/11/2012 13:49, Jason Benjamin wrote:
> Yeah, alright.  I've just found that if you mention anything about a
> library that has well established competitors, the post will tend to get
> ignored here.

I'm not sure exactly what you're referring to. (Perhaps you can link to
an existing post or discussion).

Without hearing a tone of voice, I'm not sure whether with "... will
tend to get ignored here" you're suggesting a conspiracy of silence or
merely a general lack of interest.

The people who read and post here are the people who read and post here.
And they comment according to whatever interests them or whatever
opinions they hold. That people choose to respond to a post about
Library A and not to one about Library B should not be construed as
representing any kind of behind-the-scenes action encouraging or
suppressing interest. It simply means that the people who felt like it
commented on Library A and not on Library B.

Or have I misunderstood the tenor of your comment?

TJG

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


Re: Proper place for everything

2012-11-02 Thread Tim Golden

On 02/11/2012 18:51, Jason Benjamin wrote:

On another note, it appears that Google (the only archive I can find for
this group) only has a little under 400 messages archived for this
group


  http://mail.python.org/pipermail/python-list/


http://markmail.org/search/?q=python#query:python%20list%3Aorg.python.python-list+page:1+state:facets

and doubtless others.

TJG

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


Re: Executing .exe on a remote Windows machine

2012-11-08 Thread Tim Golden
On 08/11/2012 14:25, Kevin Holleran wrote:
> Good morning,
> 
> I wrote a python script to connect out to a bunch of my remote machines
> that are running some software.  It modifies a bunch of the config files
> for me.  After making the changes, I need to restart the software.  The
> way to do this is to call an .exe passing in a argument 'restart'
>  Simply restarting services is NOT acceptable & rebooting the machine
> isn't either.
> 
> I was trying to find a way to simply call the .exe on the remote machine
> with subprocess but how can I get it to execute on the remote machine?
>  These machines do not have SSH.  

WMI can usually help with this (although there are limitations on what
you can execute via WMI). Also people recommend sysinternals' psexec.
(I've never tried it myself).

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


Re: Executing .exe on a remote Windows machine

2012-11-08 Thread Tim Golden
On 08/11/2012 15:37, Kevin Holleran wrote:
> [code]
> try:
> print("Attempting to restart Splunk...")
> subprocess.call(["psexec", "" + host, "'c:\\Program
> Files\\Splunk\\bin\\splunk.exe'", "restart"])
> [/code]
> 
> & am getting:
> 
> [output]
> Attempting to restart Splunk...
> 
> PsExec v1.98 - Execute processes remotely
> Copyright (C) 2001-2010 Mark Russinovich
> Sysinternals - www.sysinternals.com 
> 
> 
> PsExec could not start 'c:\Program Files\Splunk\bin\splunk.exe' restart
> on [IP_ADDRESS]:
> The filename, directory name, or volume label syntax is incorrect.
> [/output]
> 
> I am simply trying to restart the splunk forwarder instance
> 
> Any thoughts??

Lose the nested quotes in the .exe path; subprocess will take care of that:

subprocess.call(["psexec", "" + host, "c:\\Program
Files\\Splunk\\bin\\splunk.exe", "restart"])


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


Re: Detect file is locked - windows

2012-11-14 Thread Tim Golden
On 14/11/2012 08:55, Hans Mulder wrote:
> On 14/11/12 02:14:59, Mark Lawrence wrote:
>> On 14/11/2012 00:33, Ali Akhavan wrote:
>>> I am trying to open a file in 'w' mode open('file', 'wb'). open() will
>>> throw with IOError with errno 13 if the file is locked by another
>>> application or if user does not have permission to open/write to the
>>> file.
>>>
>>> How can I distinguish these two cases ? Namely, if some application
>>> has the file open or not.
> 
> I don't have a Windows machine at hand to try, but this might work:
> 
> if exc.errno == 13:
> if os.access('file', os.W_OK):
> print "Locked by another process"
> else:
> print "No permission to write"

No luck, I'm afraid. os.access on Windows is basically non-functional
(and would have been deprecated if I'd actually got around to doing it).
It basically checks the old-style readonly flag and that's it. IOW,
you'd return True for a file whose attributes you could read regardless
of whether you could read/write the file contents.

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


Re: Detect file is locked - windows

2012-11-14 Thread Tim Golden
On 14/11/2012 00:33, Ali Akhavan wrote:
> I am trying to open a file in 'w' mode open('file', 'wb'). open()
> will throw with IOError with errno 13 if the file is locked by
> another application or if user does not have permission to open/write
> to the file.

What version of Python are you using?

> 
> How can I distinguish these two cases ? Namely, if some application
> has the file open or not.

Can I ask what you expect to do differently in each of those cases? In
other words, if you can't access the file, you can't access it. (Not to
dismiss your question; I just wonder how you're going to handle the
different cases)

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


Re: Detect file is locked - windows

2012-11-14 Thread Tim Golden
On 14/11/2012 00:33, Ali Akhavan wrote:
> I am trying to open a file in 'w' mode open('file', 'wb'). open()
> will throw with IOError with errno 13 if the file is locked by
> another application or if user does not have permission to open/write
> to the file.
> 
> How can I distinguish these two cases ? Namely, if some application
> has the file open or not.

The Python io module calls into the MS CRT, which maps both errors
(ERROR_ACCESS_DENIED & ERROR_SHARING_VIOLATION) to posix errno EACCESS,
which is 13.


If you really need to distinguish the two situations, you'll need to
call CreateFile directly (via ctypes or the pywin32 modules or an
extension module) and then call GetLastError() to get the specific
condition.

You're far better off using this EAFP approach as, even if it were
simple to determine beforehand whether a file can be locked or read --
and it's not -- that situation could have changed by the time you
actually come to open it.

Once you've successfully got a handle to the file, that handle is valid
regardless of any later changes to the file's security.

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


Re: Detect file is locked - windows

2012-11-14 Thread Tim Golden
On 14/11/2012 11:51, Hans Mulder wrote:
> It would be nice if he could give specific error messages, e.g.
> 
> "Can't write %s because it is locked by %s."
> 
> vs.
> 
> "Can't write %s because you don't have write access."
> 
> I can't speak for Ali, but I'm always annoyed by error messages
> listing several possible cuases, such as "Can't delete file,
> because the source or destination is in use".

(I realise you're not demanding this particular behaviour from Python
but just to expand on what the obstacles are to this at present):

Speaking merely from the point of view of the current Python
implementation on Windows, there are two obstacles to this:

* Python calls into the CRT which simply returns 13 (EACCESS) for both
of these situations. Obviously, Python could do its own thing on
Windows, partly reimplementing what the CRT does anyway and giving more
precise feedback. Equally obviously, this wouldn't be a trivial exercise.

* The added information -- who's locked the file, what permissions are
in place which prevent you gaining the requested access -- is
surprisingly fiddly to get hold of and would be something of an overhead
for the majority of the time when it's not wanted. Of course, in this
hypothetical Python one could add some sort of flag to the open()
function which requested or not the additional information.

The first obstacle is more significant than the second but neither is
negligible.

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


Re: Problem with subprocess.call and windows schtasks

2012-11-18 Thread Tim Golden

On 18/11/2012 13:48, Tom Borkin wrote:

import subprocess
#subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
/ST 07:50'], shell=True)
With either call, I get this error:
C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
The system cannot find the path specified.


Generally, with subprocess call:

* Pass each element in the command line as a separate element in the 
list: ['SchTasks', '/Create', '/SC', 'ONCE', ... &c.]


* Don't use shell=True unless you're running a command which is internal 
to cmd.exe (such as dir, copy, etc.). If you're running anything which 
exists as its own .exe, you shouldn't be using shell=True.


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


Re: windows question: default click action points to wrong python version

2012-11-21 Thread Tim Golden
On 21/11/2012 08:23, Gelonida N wrote:
> Hi,
> 
> I installed python 2.6 and python 2.7 on a windows 7 machine.
> 
> At the moment Python 2.7 is the interpreter being used if I 'start' a
> python script without explicit interpreter.
> 
> I always thought, that 'repairing' Python 2.6 (reinstalling it) would
> set the default settings back to Python 2.6.
> 
> I also see with assoc / ftypes, that python 2.6. has now been configured
> as default.
> 
> However when I click on a script it is still started with 2.7.
> (even after a full restart of the machine)
> 
> This is really surprising to me.
> I thought ftype is the command to change file associations.


This area is a bit messy. There is a difference between: going to the
command line and typing "myscript.py"; and double-clicking on a file in
Explorer.

The former uses the result of merging the assoc/ftype registry keys:

   HKEY_CURRENT_USER\Software\Classes\Python.File\shell\open\command

   HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command


while the latter uses the Explorer registry keys at:


HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice


Initially, I suppose, the two are in sync. But presumably they can get
out of sync, especially if you move backwards and forwards between
associations. I haven't bothered fishing around in the Shell API but I
presume that you can reconcile the two -- or just edit the registry,
obviously.

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


Re: Problem with subprocess.call and windows schtasks

2012-11-21 Thread Tim Golden
On 20/11/2012 23:41, Tom Borkin wrote:
> Using shlex, I now have this:
> #!\Python27\python
> import os, subprocess
> path = os.path.join("C:\\", "Program Files", "Apache Group", "Apache2",
> "htdocs", "ccc", "run_alert.py")
> #subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> '/TR', path, '/ST', '23:50'])
> subprocess.call(['SchTasks', '/Create', '/SC', 'ONCE', '/TN', '"test"',
> '/TR', 'run_alert.py', '/ST', '23:50'])
> Both of the above commands throw the same error:
> ERROR: The filename, directory name or volume label syntax is incorrect.

The following works for me:

import subprocess
path = r"C:\Program Files\Apache Group\Apache2\htdocs\ccc\run_alert.py"
subprocess.call([
  'SchTasks', '/Create',
  '/SC', 'ONCE',
  '/TN', 'test',
  '/TR', path,
  '/ST', '23:50'
])

Things to note:

* I haven't added extra quoting to any of the items in the command list
which is subprocess.call's first parameter. The point about using the
list (as opposed to passing an entire string which you can also do) is
that the subprocess module can quote things as needed. If you've already
added quotes, you'll get double-quoting which you almost certainly don't
want.

* (Obviously) I've formatted the subprocess.call as I have for clarity,
especially via email. It's just a list.

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


Re: windows question: default click action points to wrong python version

2012-11-21 Thread Tim Golden

On 21/11/2012 20:53, Tony the Tiger wrote:

On Wed, 21 Nov 2012 09:23:00 +0100, Gelonida N wrote:


What am I missing?


The PATH environment variable?


Nope. PATH doesn't affect either double-clicking or running a .py file 
on the command line (unless, obviously, you run it by typing "python 
myscript.py").


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


Re: send email with bcc

2012-11-30 Thread Tim Golden

On 30/11/2012 20:25, Ed wrote:

to = '[email protected]'
bcc = '[email protected]'


[... snippage ...]


smtp.sendmail(sender, [to] + bcc, msg.as_string())


Well, you crucially don't show us the rest of the traceback. But I 
imagine you'd have got something like this:




ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> to = '[email protected]'
>>> bcc = '[email protected]'
>>> [to] + bcc
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate list (not "str") to list
>>>



which suggests, fairly clearly, that you're trying to concatenate a 
string and a list.


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


Re: Iterating over files of a huge directory

2012-12-17 Thread Tim Golden
On 17/12/2012 15:41, Chris Angelico wrote:
> On Tue, Dec 18, 2012 at 2:28 AM, Gilles Lenfant 
>  wrote:
>> Hi,
>> 
>> I have googled but did not find an efficient solution to my
>> problem. My customer provides a directory with a hge list of
>> files (flat, potentially 10+) and I cannot reasonably use
>> os.listdir(this_path) unless creating a big memory footprint.
>> 
>> So I'm looking for an iterator that yields the file names of a
>> directory and does not make a giant list of what's in.
> 
> Sounds like you want os.walk. But... a hundred thousand files? I
> know the Zen of Python says that flat is better than nested, but
> surely there's some kind of directory structure that would make this 
> marginally manageable?
> 
> http://docs.python.org/3.3/library/os.html#os.walk

Unfortunately all of the built-in functions (os.walk, glob.glob,
os.listdir) rely on the os.listdir functionality which produces a list
first even if (as in glob.iglob) it later iterates over it.

There are external functions to iterate over large directories in both
Windows & Linux. I *think* the OP is on *nix from his previous posts, in
which case someone else will have to produce the Linux-speak for this.
If it's Windows, you can use the FindFilesIterator in the pywin32 package.

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


Re: PIL or something to open EXIF Metadata with Python

2013-01-09 Thread Tim Golden
On 09/01/2013 14:45, Jose Trevino wrote:
> I am trying to load the PIL module to manage exif metadata with
> Python but have had no success. 


Try pyexiv2:

  http://tilloy.net/dev/pyexiv2/

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


Re: Query windows event log with python

2013-01-12 Thread Tim Golden

On 12/01/2013 06:09, [email protected] wrote:

I am looking to write a short program to query the windows event
log.

It needs to ask the user for input for The event type (Critical,
Error, and Information), and the user needs to be able to specify a
date since when they want to view results.

I found this piece of code to start from,


[... snip ...]

Well it looks like you have everything you need. Was there a specific 
question you wanted to ask?


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


Re: Query windows event log with python

2013-01-14 Thread Tim Golden
On 13/01/2013 05:55, [email protected] wrote:
> On Saturday, January 12, 2013 8:34:01 PM UTC+11, Tim Golden wrote:
>> On 12/01/2013 06:09, [email protected] wrote:
>> 
>>> I am looking to write a short program to query the windows event
>> 
>>> log.
>> 
>>> 
>> 
>>> It needs to ask the user for input for The event type (Critical,
>> 
>>> Error, and Information), and the user needs to be able to specify
>>> a
>> 
>>> date since when they want to view results.
>> 
>>> 
>> 
>>> I found this piece of code to start from,
>> 
>> 
>> 
>> [... snip ...]
>> 
>> 
>> 
>> Well it looks like you have everything you need. Was there a
>> specific
>> 
>> question you wanted to ask?
>> 
>> 
>> 
>> TJG
> 
> yes, I would like to run it in Command prompt and ask the user at the
> time what type and date of Event they would like to view. so i was
> wondering where in the code I could put something like
> "var=raw_input"

Ok, so your query isn't so much with accessing the event log as
with writing Python code at all. If you haven't already, could I suggest
the Python tutorial here:

  http://docs.python.org/2/tutorial/

or, if that one doesn't suit, just search for "Python tutorial" to find
something which fits your brain.

Feel free to post back here with questions once you've got started.

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


Re: Thought of the day

2013-01-15 Thread Tim Golden
On 15/01/2013 16:48, Antoine Pitrou wrote:
> Steven D'Aprano  pearwood.info> writes:
>>
>> A programmer had a problem, and thought Now he has "I know, I'll solve 
>> two it with threads!" problems.
> 
> 
> Host: Last week the Royal Festival Hall saw the first performance of a new
> logfile by one of the world's leading modern programmers, Steven
> "Two threads" D'Aprano. Mr D'Aprano.

[... snip ...]

Brilliant, just brilliant.

TJG

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


Re: Windows subprocess.call problem

2013-01-21 Thread Tim Golden
On 21/01/2013 11:25, Tom Borkin wrote:
> Hi;
> I have this code:
>  
> #!/Python27/python
> import os, subprocess, sys
> lyrics_path = "/Users/Tom/Documents/lyrics"
> os.chdir(lyrics_path)
>  
> songs = ['livin-la-vida-loca', 'whos-that-lady']
> for song in songs:
>   subprocess.call(['notepad.exe', '%s.txt' % song])
> my_songs_path = "aa english lyrics"
> os.chdir(my_songs_path)
> for song in my_songs:
>   subprocess.call(['notepad.exe', '%s.txt' % song])
>   print song
>  
> It opens the first song and hangs on subsequent songs. It doesn't open
> the next song or execute the print until I have closed the first one. I
> want it to open all in the list, one after another, so I have all those
> songs available. Please advise.

subprocess.call is a convenience for starting a process and waiting for
it to finish. If you want to start a process and carry on, use
subprocess.Popen directly (same params)

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


Retrieving the full command line

2013-01-22 Thread Tim Golden
[Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
though].

I use the python -mpackage incantation to run a package which has a
__main__.py module and which uses relative imports internally.

I'm developing under cherrypy which includes a reloader for development.
The reloader attempts to rebuild the original
command line by combining sys.executable and sys.argv and then does an
execv.

There does not appear to be any way within Python of determining the
command line I used. The combination of sys.executable and sys.argv in
this case will look like: "c:\python33\python.exe app/__main__.py". But
running this precludes the use of package-relative imports.

I can pull the command line out of the Windows API to solve this
specific problem, but is there not a gap here? Even if sys.flags somehow
indicated the -m (it doesn't) that wouldn't give me the filepath which
it used.

Have I missed something?

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


Re: Retrieving the full command line

2013-01-22 Thread Tim Golden
On 22/01/2013 14:53, Terry Reedy wrote:
> On 1/22/2013 4:24 AM, Tim Golden wrote:
>> [Python 2.7/3.3 (and hg tip) running on Windows. Not Windows-specific,
>> though].
>>
>> I use the python -mpackage incantation to run a package which has a
>> __main__.py module and which uses relative imports internally.
>>
>> I'm developing under cherrypy which includes a reloader for development.
>> The reloader attempts to rebuild the original
>> command line by combining sys.executable and sys.argv and then does an
>> execv.
>>
>> There does not appear to be any way within Python of determining the
>> command line I used. The combination of sys.executable and sys.argv in
>> this case will look like: "c:\python33\python.exe app/__main__.py". But
>> running this precludes the use of package-relative imports.
> 
> If I understand right, the reloader should be updated to translate
> 'x/__main__.py' to '-m x'. Filenames of form'__x__' are reserved, in a
> sense, like similar identifiers in programs, and '__main__.py' should
> not be used for a file meant to executed directly.

To be clear: it's Python itself, not the reloader, which is coming up
with __main__.py. sys.executable is "c:\python33\python.exe" and
sys.argv is ['c:\path\to\__main__.py'] for a program which has been
started by "c:\python33\python.exe -mpath\to".

Obviously, there is any number of ways around this specific issue,
including what you suggest: a canonical rewrite of "python
path\to\__main__.py" into "python -mpath\to". But it's not clear to me
that this rewrite should be the responsibility of calling code.

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


Re: Retrieving the full command line

2013-01-23 Thread Tim Golden
On 23/01/2013 03:58, Steven D'Aprano wrote:
> Currently, if I have a package __main__.py that prints sys.argv, I get 
> results like this:
> 
> steve@runes:~$ python3.3 /home/steve/python/testpackage/__main__.py ham 
> spam eggs
> ['/home/steve/python/testpackage/__main__.py', 'ham', 'spam', 'eggs']
> 
> 
> which is correct, that's what I gave on the command line. But:
> 
> steve@runes:~$ python3.3 -m testpackage ham spam eggs
> ['/home/steve/python/testpackage/__main__.py', 'ham', 'spam', 'eggs']
> 
> 
> The second example is lying. It should say:
> 
> ['-m testpackage', 'ham', 'spam', 'eggs']

Thanks for the input, Steven & Oscar.

Apologies for the confusion over my initial example. Of course, -m runs
something on sys.path, not something in the filesystem as such. I
confused myself because, running on Windows where the current directory
is on the path, I sit in c:\path\to and do python -mapp

Now I look harder, this discussion is basically issue14208:

  http://bugs.python.org/issue14208

so I'll probably go and contribute over there.

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


Re: Retrieving the full command line

2013-01-24 Thread Tim Golden
On 24/01/2013 10:06, Oscar Benjamin wrote:
> On 24 January 2013 04:49, Steven D'Aprano
>  wrote:
> [SNIP]
>>
>> Contrariwise, I don't believe that there is currently *any* way to
>> distinguish between running a script with or without -m. That should be
>> fixed.
> 
> As I said earlier in the thread, the __package__ module global
> distinguishes the two cases:
> 
> ~$ mkdir pkg
> ~$ touch pkg/__init__.py
> ~$ vim pkg/__main__.py
> ~$ cat pkg/__main__.py
> import sys
> if __package__ is None:
> cmdline = [sys.executable] + sys.argv
> else:
> cmdline = [sys.executable, '-m', __package__] + sys.argv[1:]
> print(cmdline)
> ~$ python pkg/__main__.py arg1 arg2
> ['q:\\tools\\Python27\\python.exe', 'pkg/__main__.py', 'arg1', 'arg2']
> ~$ python -m pkg arg1 arg2
> ['q:\\tools\\Python27\\python.exe', '-m', 'pkg', 'arg1', 'arg2']

Reasonable (and thanks for the clear example), but it doesn't work
if the package which is reconstructing the command line the package
which was the target of the original command line. In my case,
I'm making use of the cherrypy reloader, whose __package__ is
cherrypy.process. But the command which invoked the program was
python -m myapp.

ie I'm issuing "python -m myapp". In myapp.__main__ I'm importing
cherrypy, itself a package, and somewhere in cherrypy.whatever there is
code which attempts to reconstruct the command line.

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


Re: Retrieving the full command line

2013-01-24 Thread Tim Golden
On 24/01/2013 10:56, Tim Golden wrote:
> if the package which is reconstructing the command line the package
> which was the target of the original command line.


Sorry:

  if the package which is reconstructing the command line *is not*
  the package which was the target of the original command line.

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


Re: Retrieving the full command line

2013-01-24 Thread Tim Golden
On 24/01/2013 11:30, Oscar Benjamin wrote:
> I don't really understand what your spec is. Why do you need to
> inspect this information from sys.argv? Can you not just always use
> 'python -m pkg' as your entry point?

Sorry about the confusion. I think my original point was simply one
of surprise that sys.argv wouldn't essentially mirror the elements
of the command line which I used to get there.
The specifics of my use-case weren't really too important.

For completeness, I'm talking about the cherrypy Autoreloader which
attempts to restart (via execv) whatever process was responsible for
loading it in the first place, via an identical or equivalent command
line. The current (cherrypy) code simply joins sys.executable and
sys.argv but this fails in the face of python -m as we have seen.

The cherrypy package has no especial knowledge of the structure of the
application which imported it and so must piece together the command
line somehow. Clearly, I can take various approaches of the sort
which you've outlined, or subclass the reloader, or fetch the original
command line from the OS, etc. It's not that this is a showstopper,
merely slightly surprising. (To me).

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


Re: Retrieving the full command line

2013-01-24 Thread Tim Golden
On 24/01/2013 15:28, Oscar Benjamin wrote:
> On 24 January 2013 13:45, Tim Golden  wrote:
>> On 24/01/2013 11:30, Oscar Benjamin wrote:
>>> I don't really understand what your spec is. Why do you need to
>>> inspect this information from sys.argv? Can you not just always use
>>> 'python -m pkg' as your entry point?
>>
> [SNIP]
>>
>> For completeness, I'm talking about the cherrypy Autoreloader which
>> attempts to restart (via execv) whatever process was responsible for
>> loading it in the first place, via an identical or equivalent command
>> line. The current (cherrypy) code simply joins sys.executable and
>> sys.argv but this fails in the face of python -m as we have seen.
>>
>> The cherrypy package has no especial knowledge of the structure of the
>> application which imported it and so must piece together the command
>> line somehow. Clearly, I can take various approaches of the sort
>> which you've outlined, or subclass the reloader, or fetch the original
>> command line from the OS, etc. It's not that this is a showstopper,
>> merely slightly surprising. (To me).
> 
> Ok I understand. Then I guess you want:
> 
> import __main__
> pkg = __main__.__package__

Brilliant. Never thought of importing __main__. Thanks.

For the benefit of anyone still watching, the code (which has to be
compatible back to 2.3) looks something like this:


import __main__

# [.. .snip ...]

try:
is_package = bool(__main__.__package__)
except NameError:
is_package = False
if is_package:
args = [sys.executable, '-m', __main__.__package__] + sys.argv[1:]
else:
args = [sys.executable] + sys.argv

os.chdir(_startup_cwd) # avoids relative/absolute issues
os.execv(args[0], args)



I don't pretend it's foolproot, but it certainly works for my particular
case. Nor have I considered it against all the cases identified in PEP
432: http://www.python.org/dev/peps/pep-0432/#configuring-sys-argv

TJG

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


Re: Retrieving the full command line

2013-01-24 Thread Tim Golden
On 24/01/2013 16:53, Oscar Benjamin wrote:
>> Does it work if you use the -m option to run a module rather than a script?
> 
> Sorry that was written incorrectly. I meant to say: does it work when
> a module is directly on sys.path rather than as a submodule of a
> package? In this case __package__ is set to the empty string if run
> with -m or None if run with a direct path. So the check needs to be
> "__package__ is not None" rather than "bool(__package__)".

The answer is: it depends. Given the code I outlined earlier:

A package-based module run via -m (python -m package.module) works
as described (including the implicit __main__ module, my
primary use-case).

A module run from the filesystem (python c:\path\to\module.py) works
by dropping through through to the not is_package logic branch.

A module run via -m (python -m module) actually works by accident,
because it too drops through to the not is_package branch and is
rerun with its full filesystem path. This doesn't have the same
problems as running a package from the filesystem because relative
imports aren't an issue. I don't know if there are any other differences
between python -mmodule and python c:\path\to\module.py.

As you say, a more refined check could determine a blank __package__
as opposed to a None __package__. But this code (cherrypy) must also
cope with version of Python before 2.6 which didn't even have a
__package__ attribute, muddying the waters that little bit further.

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


Re: Retrieving the full command line

2013-01-24 Thread Tim Golden

On 24/01/2013 20:01, Oscar Benjamin wrote:

On 24 January 2013 17:13, Tim Golden  wrote:

A package-based module run via -m (python -m package.module) works
as described (including the implicit __main__ module, my
primary use-case).


Does it work in the "python -m package.module" case? It looks to me as
if the code below will run "python -m package" instead. I think you
need to split the module name out of sys.argv[0] and put that into the
command line as well.


Good catch. Unless I am, once again, mistaken, I can't see any way
of recreating a dotted module path which is running as a main module
without the somewhat fragile expedient of walking back up the
filepath and hoping that nothing strange has happened to the imports
on the way down? (pywin32: I'm looking at you!)

And we haven't even touched on zipped archives or other invented
loaders (pulling modules from a database or direct from the internet).

I think, though, that the point which with I started this thread is
still valid: that to reconstruct the command line with which Python
was started, you need an increasingly awkward set of conditions and
checks. As opposed to asking the interpreter: please give me the command
line which started you.

How common that particular requirement actually is, I couldn't say.
But the two cases which clearly could use it are: the reloader
setup we've been discussing here; and the apply-new-version-and-restart
which I'm hoping to use in another setup at work.

Thanks for all the help and input on this, Oscar

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


Re: Cycle around a sequence

2012-02-08 Thread Tim Golden

On 08/02/2012 08:26, Mark Lawrence wrote:

On 08/02/2012 01:26, Dennis Lee Bieber wrote:

On Wed, 08 Feb 2012 01:10:28 +, Mark Lawrence
 wrote:


I'm looking at a way of cycling around a sequence i.e. starting at some
given location in the middle of a sequence and running to the end before
coming back to the beginning and running to the start place. About the
best I could come up with is the following, any better ideas for some
definition of better?

PythonWin 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
for further copyright information.

from itertools import chain
a=range(10)
g = chain((a[i] for i in xrange(4, 10, 1)), (a[i] for i in
xrange(4)))
for x in g: print x,

...
4 5 6 7 8 9 0 1 2 3




How large a sequence and, more important, is it fully known at the
start...


a = range(20)
a

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

a_shift = a[5:] + a[:5]
a_shift

[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4]




IOWs, just slice and join: tail first, then front-end.



The sequences are small and the start is always known but the function
that uses this is called thousands of times so I was trying to avoid
building lists if at all possible.



I'm not an itertools expert, but does this do what you want?
(Untested - I might well be off by one)


import itertools

sequence = range (100)
split = 70
rslice = itertools.islice (sequence, split, len (sequence))
lslice = itertools.islice (sequence, split)

repeater = itertools.cycle (itertools.chain (rslice, lslice))



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


Re: check if directory is writable in a portable way

2012-02-28 Thread Tim Golden

On 28/02/2012 10:07, Andrea Crotti wrote:

How should I check if I can create files in a directory?

I tried to simply check if the directory is writeable with this function:

def is_writable(name):
"""Return true if the file is writable from the current user
"""
return os.access(name, os.W_OK)

but that doesn't work at all on Windows, because for example if I create
a new directory
and do os.access(dirpath, os.W_OK) it returns false, even if I can
create files inside without problems.

So maybe the only solution that works is something like
try:
open(path.join('temp', 'w'))
except OsError:
return False
else:
os.remove(path.join('temp'))
return True

would it make sense?



This is remarkably complicated to do on Windows by
checking Security APIs etc. If the try:except dance
works for you, I recommend that you use it. (In the
past, people who have asked this question have not
wanted to use try-except because they didn't want the
overhead of creating even a zero-length file)

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


Re: check if directory is writable in a portable way

2012-02-28 Thread Tim Golden

On 28/02/2012 12:01, Andrea Crotti wrote:

On 02/28/2012 11:34 AM, Tim Chase wrote:

On 02/28/12 04:07, Andrea Crotti wrote:

How should I check if I can create files in a directory?

So maybe the only solution that works is something like
try:
open(path.join('temp', 'w'))
except OsError:
return False
else:
os.remove(path.join('temp'))
return True


It depends on the system & location. It's possible to set up
directories with permissions that allow you to create files but not
delete them, in which case you'd either (1) create the file and
possibly fail on subsequent tests because the file already exists; or
(2) litter the directory with tmpnam()-like results that you were
unable to delete.

It's ugly, I've encountered it, and haven't found a good universal
solution myself.

-tkc



That's really ugly right, didn't think about this possibility.
Well it's not a really critical part of my code, so I guess it's fine
with the try-except dance..

But isn't there (or should there be) a windows-related library that
abstracts this horrible things?


Well I maintain winsys [1] which does help out in admin-y tasks
in general on Windows, but I'm afraid I've never had the need
to scratch this particular itch, and the API solution is messier
than you might think, and more fiddly to get right. If I ever
get enough moments together I might do it, but patches are
always looked upon kindly :)

TJG

[1] https://github.com/tjguk/winsys
--
http://mail.python.org/mailman/listinfo/python-list


Re: Windows Contextmenu

2012-03-13 Thread Tim Golden

On 13/03/2012 09:41, Szabo, Patrick (LNG-VIE) wrote:

I wrote the following Script which I want to run from the open with
contextmenu in Windows.

For that purpose I used py2exe to make an exe out of it.


[... snip ...]



Now the script runs fine but I don’t get all arguments from sys.argv.

No mather how many files I mark in the explorer I only get one as an
argument.


You're missing out vital information:

* How have you attached this code to the context menu? What was
the exact registry entry (or other method) you used?

* Does it work as native Python (ie without the py2exe layer)?

* Presumably the same issue occurs if you simply have: print sys.argv
  on its own (ie it's nothing to do with your loop and later code)

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


Re: Python is readable

2012-03-15 Thread Tim Golden

On 15/03/2012 14:19, Kiuhnm wrote:

On 3/15/2012 15:06, Mark Lawrence wrote:

On 15/03/2012 11:48, Kiuhnm wrote:

BTW, aren't those ':' redundant?

Kiuhnm


Nope.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("filename", "w") as f
File "", line 1
with open("filename", "w") as f
^
SyntaxError: invalid syntax


Ok, so they're mandatory, but I was mainly talking of design. Why are
they needed?

Kiuhnm


http://docs.python.org/faq/design.html#why-are-colons-required-for-the-if-while-def-class-statements

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


Re: is this foolish?

2012-04-12 Thread Tim Golden
On 12/04/2012 10:35, Cameron Simpson wrote:
> I've found myself using a Python gotcha as a feature.

Have a look at Peter Inglesby's lightning talk from a
recent London Python Dojo:

  http://inglesp.github.com/2012/03/24/mutable-default-arguments.html

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


Re: Recruiting for Python Developer - Perm

2012-05-22 Thread Tim Golden
Someone will soon pop up and tell you that job ads are
unwelcome on this list and that you should post to
the Python Jobs board --> http://www.python.org/community/jobs/

However, the python-uk list is probably a better place for
UK-focused jobs, as long as you have something which is
definitely Python and definitely in the UK:

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

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


Re: How do you copy files from one location to another?

2011-06-17 Thread Tim Golden

On 17/06/2011 06:06, John Salerno wrote:

Based on what I've read, it seems os.rename is the proper function to
use, but I'm a little confused about the syntax. Basically I just want
to write a simple script that will back up my saved game files when I
run it. So I want it to copy a set of files/directories from a
location on my C:\ drive to another directory on my E:\ drive. I don't
want to rename or delete the originals, just move them. I also want
them to automatically overwrite whatever already happens to be in the
location on the E:\ drive.

Is os.rename the proper function for this? Mainly I was because the
Module Index says this:

"On Windows, if dst already exists, OSError will be raised even if it
is a file.."

so it sounds like I can't move the files to a location where those
file names already exist.


For a Windows-only Q&D, you could use the pywin32 win32file module
which exposes the MoveFileEx[W] API:


import win32file

win32file.MoveFileExW (
  "c:/temp/blah.txt",
  "c:/temp/blah2.txt",
  win32file.MOVEFILE_REPLACE_EXISTING
)



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


Re: SQL Server 2008R2 databases via Python 2.7 and Windows XP and higher

2011-06-17 Thread Tim Golden

On 17/06/2011 16:01, [email protected] wrote:

Looking for some real-world advice on what is the best way to access MS
SQL Server 2008R2 databases via Python 2.7 running under Windows XP,
Vista, and Windows 7 and Windows Server 2005 and 2008.
Based on my research, here's my list of choices:
mxODBC
http://www.egenix.com/products/python/mxODBC/
pyOdbc 2.1.8
http://code.google.com/p/pyodbc
pyMSSQL 1.02
http://code.google.com/p/pymssql


I use pyodbc (and have done for some years) without any problems
at all. Barring the one or two issues I've logged which are
being addressed. (grin). We use pretty much exactly the mixture
of platforms you're describing.

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


Re: User Authentication

2011-06-22 Thread Tim Golden

On 22/06/2011 14:34, Anurag wrote:

Hi All,

I am working on application which needs to do a authentication against
LDAP, if LDAP not installed then local system account (administrator
user in windows and root user in Linux). This should work on both
Windows and Linux.

Which library I should use for that.


python-ldap seems the obvious choice:


  http://www.python-ldap.org/

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


Re: connect windows share

2011-06-22 Thread Tim Golden

On 22/06/2011 19:38, Travis Altman wrote:

I want to be able to connect to a windows share via python.  My end goal
is to be able to recursively search through windows shares.  I want to
do this in Linux as well.  So given a share such as \\computer\test I
would like to search through the test directory and any sub directories
for any file names of interest.  What's the best way of going about
this?  I know LDAP / AD creds may play an essential part in this as
well.  Thanks for your input.


Unless there's more to this than meets the eye, the answer's
really easy: just do it.

On Windows, you just do, eg:

import os

for dirpath, dirnames, filenames in os.walk (r"\\computer\test"):
  # do stuff with stuff

Presumably on Linux you have to use whatever mechanism allows
you to address Windows shares in the equivalent way. Maybe

os.walk ("smb://computer/test") ?? I'm not really a Linux person,
I'm afraid

TJG

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


Re: Python 2.7 and cmd on Windows 7 64 (files lost)

2011-06-23 Thread Tim Golden

On 23/06/2011 07:33, Michel Claveau - MVP wrote:

Hi!

(sorry for my bad english...)

On Win 7 64 bits:
   Command-Line
   CD \Python27
   dir C:\Windows\System32\SoundRecorder.exe:==>  OK
   Python.exe


import os
os.system("dir C:\\Windows\\System32\\SoundRecorder.exe")


==>  Do not found the file !!!

and os.system("cmd /k")  then  "dir C:\Windows\System32\SoundRecorder.exe"  do 
not found
anyway.

But:
   {Ctrl-Z} in Python
   then  dir C:\Windows\System32\SoundRecorder.exe  run OK

Therefore, is the problem only in Python?


Certain commands, including "dir" and "copy" are not executables
in their own right, but merely subcommands of cmd.exe. You've
got two options in Python:

  os.system (r"cmd /c dir c:\windows")

or:

  subprocess.call (["dir", "c:\\windows"], shell=True)

which basically does it for you behind the scenes.

I hope that helps..

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


Re: User Authentication

2011-06-23 Thread Tim Golden

On 23/06/2011 06:02, Anurag wrote:

On Jun 22, 7:01 pm, Adam Tauno Williams
wrote:

On Wed, 2011-06-22 at 06:34 -0700, Anurag wrote:

Hi All,



I am working on application which needs to do a authentication against
LDAP, if LDAP not installed then local system account (administrator
user in windows and root user in Linux). This should work on both
Windows and Linux.


See python-ldap


I looked into python-ldap, it supports ldap authentication. But I
didn't find anything that support local system account authentication
when no LDAP both in windows and Linux.


If you want local system authentication on Windows, you're going
to need something like this:


http://timgolden.me.uk/python/win32_how_do_i/check-a-users-credentials.html


Ond more thing, somebody suggested me to use PAM. Is it a good choice
and whether it supports both Windows and Linux? Please let me know
which is best to use.


I can't speak for Linux, but I can guarantee that PAM won't
take you anywhere on Windows :)

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


Re: Python 2.7 and cmd on Windows 7 64 (files lost)

2011-06-23 Thread Tim Golden

On 23/06/2011 09:08, Thorsten Kampe wrote:

* Tim Golden (Thu, 23 Jun 2011 08:31:26 +0100)


Certain commands, including "dir" and "copy" are not executables
in their own right, but merely subcommands of cmd.exe.


Right, "internal commands".


You've got two options in Python:

os.system (r"cmd /c dir c:\windows")


os.system automatically runs a shell (cmd) - see the documentation.


Thanks: should have checked my sources before writing :(

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


Re: User Authentication

2011-06-23 Thread Tim Golden

On 23/06/2011 16:07, Anurag wrote:

My application is a web based application for both windows and Linux.
The web part is developed using Django. So if Python does not support
it then any support for local sytem account authentication in Django?

I am looking for a common library for both Linux and Windows. Any help
will be Gr8


The two systems are sufficiently different in this kind of area
that any "common library" will basically consist of code like
this (hand-wavey):



import sys

if sys.platform.startswith ("win"):
  import win32security
  def is_valid_user (username, password):
if win32security.LogonUser ( ... )

elif sys.platform.startswith ("linux"):
  import pam
  def is_valid_user (username, password):
if pam.validate_user (...)



It's possible someone's put something together already,
but I'm not aware of anything.

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


Re: windows 7 create directory with read write execute permission for everybody

2011-06-27 Thread Tim Golden

On 26/06/2011 21:57, Gelonida wrote:

Hi,

What do I have to do under python windows to create a directory with all
permissions, such, that new files / directories created below will
inherit the permissions.


The reason I am asking is, that I'd like to create a directory structure
where multiple users should be allowed to read / write / create files
and directories.


Alternatively it would be even better to specify exactly which users
should be allowed to access the directory tree.

I never used / modified Windows file permissions except once or twice
via explorer. I'm thus a little shaky with Microsoft's file permissions.


Have a look at this to see if takes you anywhere:

  http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.html

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


Re: How to get a dateiled process information on windows?

2011-07-01 Thread Tim Golden

On 01/07/2011 21:06, Leandro Ferreira wrote:

I need to write an application that monitors the memory consumption of
a process, there is some library that facilitates this work?
I searched on google found nothing more interesting.


Have a look at WMI

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


Re: Require information on python API for Subversion related work

2011-08-02 Thread Tim Golden

On 02/08/2011 14:02, Shambhu Rajak wrote:

I need an api that can be used to do following operations on Subversion
repository tool:

1.Create branch

2.Check out

3.Check in

4.Merge



http://pysvn.tigris.org/

(which is, by the way, the first Google hit for "Python Subversion 
bindings")


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


Re: PyWhich

2011-08-05 Thread Tim Golden

On 05/08/2011 14:51, Steven D'Aprano wrote:

Tim Chase wrote:


On 08/04/2011 07:34 PM, Steven D'Aprano wrote:

Billy Mays wrote:

#!/usr/bin/python


I believe the recommended, platform independent hash-bang line is

#!/usr/bin/which python


I think you mean

#!/usr/bin/env python



Doh! I *always* conflate env and which. Thank you for the correction.


And there I was thinking you were making a sly and ironic point
about using a utility to find something on the path in order to
run a utility which finds something on the path...

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


Re: Directions on accessing shared folder in windows network

2011-08-11 Thread Tim Golden

On 10/08/2011 21:43, Christian Heimes wrote:

Am 10.08.2011 21:52, schrieb Ameet Nanda:

Hi,

Can anyone point me to a way to access windows shared folders from the
network using a python script. I tried accessing using open, which is
mentioned to work perfectly on the web, but it gives me following errors


The open() function wraps the fopen() function, which doesn't support
UNC, just local paths. You have to mount the network share first (assign
a drive letter).


Christian, with respect: what are you talking about?


print open (r"\\localhost\c$\python27\python.exe").read (100)



The OP's problem is that he's trying to call open () on the
share itself, not on a file within it.

TJG

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


Re: How to Check Write Access of a Folder on Windows

2011-08-12 Thread Tim Golden

On 12/08/2011 11:41, Ayaskanta Swain wrote:

Hi,

I have a requirement where I need to check the write permissions on a
directory on Windows platform. I don’t want to use the python function
os.access( ), since it does not work correctly on Windows. It is giving
incorrect results to me.

Another option of actually creating a temporary file inside the folder &
then deleting it to check whether the user has write permissions or not
is causing performance issues. It is slowing down our application when
we have to check the permissions on hundreds of folders. It also changes
the last access time stamp of the folder.

So is there any other way such as using Win32API or Win32 security
modules to check the permissions?


In general, yes. Depends how finnicky you want to get. I take it from
your question that you're pretty much unfamiliar with the Windows
Security APIs and structures? There's a quite a bit of information
around on the subject, but have a look at this first which is at
least Python-oriented:

http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.html

The trouble is that do what you want, you need to use the AccessCheck
API (or emulate its functionality) and this isn't currently exposed by
the pywin32 modules. You could run it up yourself with ctypes or by
creating a quick extension for the purpose. Alternatively, as I say,
you could emulate by scanning the ACLs/ACEs and trying to apply some
suitable logic. Frankly, I'd have thought this was harder work :)

Hope that helps

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


Re: Windows service in production?

2011-08-16 Thread Tim Golden

On 16/08/2011 05:32, snorble wrote:

Anyone know of a Python application running as a Windows service in
production? I'm planning a network monitoring application that runs as
a service and reports back to the central server. Sort of a heartbeat
type agent to assist with "this server is down, go check on it" type
situations.


Don't know what it'll take to inspire you with confidence, but I have
several Python services running here without a hitch.
The longest have been running for about three years -- not without
a stop, since they have to be restarted for reasons external to the
service itself.

There's no py2exe involved, just the pywin32 service wrappers. (The
apps in question are also set up to run standalone for testing etc.).
They're mostly around a helpdesk system: one app ingests email requests
to the helpdesk; another post-processes call changes, currently to
send out email alerts to interested parties; another triggers alarms
on calls for various purposes, etc.

I don't claim they're the most sophisticated pieces of code on Earth,
but it doesn't sound like you're after anything spectacular either.

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


Re: Windows service in production?

2011-08-17 Thread Tim Golden

On 16/08/2011 15:46, snorble wrote:

Interesting. Normally I would use py2exe then do "myapp.exe -install"
to install the app as a service. How do you handle installing the
service? Also what does the service show under the properties, for the
executable? "python.exe script.py" or something else?


To install, I simply invoke the Command-line handler which comes with 
the pywin32 service utils:


if __name__ == '__main__':
  win32serviceutil.HandleCommandLine (Service)

(I imagine that's what py2exe's doing for you behind the scenes).

The executable shows as pythonservice.exe. The short and long
display names can of course be whatever you like.

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


Re: How to Check Write Access of a Folder on Windows

2011-08-17 Thread Tim Golden

On 16/08/2011 13:38, Ayaskant Swain wrote:

Hi Tim,

Thanks for your reply. It seems this issue is related to python bug
-http://bugs.python.org/issue2528
But the patch code looks complex to me. I want to make changes only
in my python script which will read an user given directory path&
check it's write access. What is the use of posixmodule.c file?


Well.. the changes to posixmodule.c, had they been accepted, would
have meant that a call to os.access on Windows would have done the
work to determine whether or not you truly had access. As it is,
that issue has been "demoted" to a doc-and-deprecate bug.

So you either need to do the same sequence of actions (more or
less) or to simulate its effect. Certainly you can retrieve the
ACEs in the DACL via the GetFileSecurity call, and you're then
going to have to compare the effect of all the (possibly
quite complex and inherited) ACEs on the logged-in user's ability
to view the directory.

The ACEs might refer directly to the user or (more likely)
indirectly via groups, including builtins like "Domain Users"
or "Administrators". So you'd have to work out whether your user
was in one of those groups, possibly indirectly as groups can
contain groups.

Certainly all of this is do-able but it won't be trivial. I'm
sorry I can't be more helpful but I haven't had a need for this
functionality myself (even the patch to issue2528 was in response
to someone else's need similar to yours).

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


Re: List spam

2011-08-18 Thread Tim Golden

On 18/08/2011 13:58, Jason Staudenmayer wrote:

I really like this list as part of my learning tools but the amount
of spam that I've been getting from it is CRAZY. Doesn't anything get
scanned before it sent to the list?


I haven't seen any significant quantity of spam on the list for ages. 
(The occasional one does get through although I can't remember the last).


I always access it as a mailing list, and I seem to recall that
the newsgroup feed isn't filtered the same way as the mailing list is.

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


Re: To cmd.exe

2011-08-22 Thread Tim Golden

On 22/08/2011 14:21, aba ca wrote:

How can I send to cmd.exe "netstat -an"?


Look at the subprocess module, and especially the .check_output
convenience function:

  http://docs.python.org/library/subprocess.html#subprocess.check_output

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


Re: Setting the time in Win7

2011-08-22 Thread Tim Golden

If memory serves, you need to enable a specific privilege to
set the time in Vista+. Just a moment...

Have a look here:

  http://support.microsoft.com/kb/300022

and look for SeSystemtimePrivilege generally. Sorry; I'm
a bit rushed at the moment. Feel free to post back if
that isn't clear

TJG

On 22/08/2011 17:35, Bob Greschke wrote:

Permissions!

We're running in an account as an administrator (the only account on the
laptops) and the program just calls system(time ) and system(date
) after reading it from a connected GPS receiver. I've fiddled with
everything I could find in the registry and with the secpol.msc program
and there doesn't seem to be any reason that it can't set the time, but
it can't. Any ideas?

Thanks!

Bob



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


Re: Setting the time in Win7

2011-08-23 Thread Tim Golden

On 22/08/2011 20:42, Bob Greschke wrote:

Several people have been hacking away on this computer we are testing
on, so I'm not sure what settings -- other than all of them -- have been
messed with, but popen("time ...") seems to work, but system("time ...")
does not. I'm going to restore the machine to its original state and see
what happens.


Hoping that this helps: you can programatically set the system time
from within Python by using the pywin32 modules, or ctypes if you
prefer. The code below works for an already-elevated command prompt
by enabling the SystemTime privilege and (crudely) moving the time
forward by five minutes by way of showing what's happening before
resetting it back.

I've commented out the actual SetSystemTime calls just in case anyone
cuts-and-pastes indjudiciously. Ideally you should disable the
privilege afterwards but I've left that out so as not to clutter
the example.


import os, sys

import win32api
import win32security
import ntsecuritycon

hToken = win32security.OpenProcessToken (
  win32api.GetCurrentProcess (),
  ntsecuritycon.MAXIMUM_ALLOWED
)
time_privilege = win32security.LookupPrivilegeValue (None, 
win32security.SE_SYSTEMTIME_NAME)

win32security.AdjustTokenPrivileges (
  hToken, 0,
  [(time_privilege, win32security.SE_PRIVILEGE_ENABLED)]
)

current_time = win32api.GetSystemTime ()
print "Current time:", current_time
new_time = list (current_time)
new_time[5] += 5
## print win32api.SetSystemTime (*new_time)
print "Current time:", win32api.GetSystemTime ()
## print win32api.SetSystemTime (*current_time)
print "Current time:", win32api.GetSystemTime ()



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


Re: Detecting Ctrl-Alt-Del in Windows

2011-09-02 Thread Tim Golden

Obviously, this is a windows-based question.  I know that Ctrl-Alt-Del
is handled deep inside the OS, and I'm not trying to interrupt that.
But is there some way to detect that a C-A-D has been pressed?


Others have pointed out that this shouldn't really be possible for
reasons of security. (And I agree). However, if what you're really
after is to detect a session switch or a logon then this might
be of some use:

http://timgolden.me.uk/python/win32_how_do_i/track-session-events.html

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


Re: Switching between Python releases under Windows

2011-03-08 Thread Tim Golden

On 08/03/2011 14:55, Edward Diener wrote:

I have multiple versions of Python installed under Vista. Is there any
easy way of switching between them so that invoking python and file
associations for Python extensions files work automatically ?


Well, the answer depends a bit on how au fait you are with fiddling
with env vars etc. but, if it helps, this script:

  https://winsys.googlecode.com/svn/trunk/random/associate.py

will set the version of Python which runs it as the default.
If you're running on Vista/W7, you'll probably need to run it
with a param of "user" or inside an Elevated command prompt.

TJG

(Won't work w/ Python 3 without a bit of tweaking)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Switching between Python releases under Windows

2011-03-08 Thread Tim Golden

On 08/03/2011 15:58, Tim Golden wrote:

On 08/03/2011 14:55, Edward Diener wrote:

I have multiple versions of Python installed under Vista. Is there any
easy way of switching between them so that invoking python and file
associations for Python extensions files work automatically ?


Well, the answer depends a bit on how au fait you are with fiddling
with env vars etc


But essentially involves:

* Adding c:\pythonxy and c:\pythonxy\script to PATH

* assoc .py=python.file [probably already done]

* python.file="C:\Pythonxy\python.exe" "%1" %*


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


Re: error in exception syntax

2011-03-09 Thread Tim Golden

On 09/03/2011 6:12 PM, Aaron Gray wrote:

On Windows I have installed Python 3.2 and PyOpenGL-3.0.1 and am getting the
following error :-

 File "c:\Python32\lib\site-packages\OpenGL\platform\win32.py", line 13
   except OSError, err:
 ^

It works okay on my Linux machine running Python 2.6.2.


In short:

  http://docs.python.org/release/3.0.1/whatsnew/3.0.html#changes-to-exceptions

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


Re: Creating a very simple revision system for photos in python

2011-03-11 Thread Tim Golden

On 11/03/2011 16:05, Chris Hulan wrote:

On Mar 11, 9:56 am, Thomas W  wrote:

I`m thinking about creating a very simple revision system for photos
in python, something like bazaar, mercurial or git, but for photos.
The problem is that handling large binary files compared to plain text
files are quite different. Has anybody done something like this or
have any thoughts about it, I`d be very grateful. If something like
mercurial or git could be used and/or extended/customized that would
be even better.

We are talking about large numbers of photos and some of them are
large in size as well, but the functionality does not have to be a
full fledged revision system, just handle checking out, checking in,
handling conflicts, rollbacks etc, preferrably without storing
complete copies of the files in question for every operation.

Thanks for any input. :-)


Most traditional revision systems excel at managing text, but suck at
binary.
I recall that Picassa has a revision system

It occurs to me you could use Uuencoding to make binaries more
amendable to
handling by text-oriented revision systems


I'm not sure there's much point in doing that. Certainly Subversion,
and I imagine the other main RCS, handle binary data perfectly well;
I mean, they don't stop when they come across a NUL byte or anything
like that. You can't do much with the result except retrieve it, but
I'm not sure that uuencodeing (or any other encoding) adds anything
there, either.

FWIW TortoiseSvn offers an image-diff utility which superimposes the
two versions of an image with an alpha blend (if that's what it's
called). It's basically the electronic equivalent of holding two
sheets of paper up to the light. Don't know if this helps the OP,
mind you.

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


Re: How to schedule execution of code?

2011-03-15 Thread Tim Golden

On 15/03/2011 07:16, Virgil Stokes wrote:

Suppose that I have some Python code (vers. 2.6) that has been converted
into an *.exe file and can be executed on a Windows (Vista or 7)
platform. What can one do to have this *.exe executed at a set of
specific times each day?


Well, once you've got an .exe, the question isn't really Python-specific
any more. (Even without the .exe it's not really Python-specific...)
Use the Windows Scheduler. You can schedule a program to run at
certain times and on startup. When it fires up, it can check when
it was last run (by writing to a file / registry on firing up)
and deciding whether or not it needs to run.

The alternative is to have it install as a service and then run
its own scheduler loop, but that doesn't offer any real advantage:
you're just reinventing the Windows Task Scheduler.

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


Re: silent install python.msi

2011-03-15 Thread Tim Golden

On 15/03/2011 08:30, Cornelius Kölbel wrote:

I am using the python.msi (at the moment 2.6) in a project and want to
deploy the python package smoothly and nearly automatically.

So my question is, if the python.msi provides some parameters, to pass
the information like which components to install and the install path,
without popping up all the dialogs?
I do not want to use the default install path!


Have a look here:

  http://www.python.org/download/releases/2.4/msi/

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


Re: python script to find Installed programs in Uninstall folder in registry

2011-03-15 Thread Tim Golden

On 15/03/2011 03:42, KishoreRP wrote:

I am working on creating a python script to find Installed programs in
Uninstall folder in registry, the script works perfectly fine on 32
bit machines but errors out with a wmi error on 64 bit machines.


You don't say what the error is (and your snippet doesn't run without
some alterations) so would you mind showing just enough code for
the error to occur plus a cut-and-paste of the traceback?

> Am not able to get hold of a wmi module for python on 64 bit machines.
> Is there one at all?

The wmi module is pure Python, relying only on Python & the pywin32
extensions. If you have both of those installed as 64-bit you can
just drop the wmi.py module into the right place and go from there.


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


Re: os.path.walk() to get full path of all files

2011-03-17 Thread Tim Golden

On 17/03/2011 08:58, Laurent Claessens wrote:



file_list = []
for root, _, filenames in os.walk(root_path):
for filename in filenames:
file_list.append(os.path.join(root, filename))



What does the notation "_" stands for ? Is it a sort of /dev/null ?

I know that in the terminal it represents the last printed text.


It's a convention for saying "I don't really care about this
particular value which is returned from that function". You
could, at your whim, use "dontcare" or "x" or whatever.

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


Re: Remove all directories using wildcard

2011-03-18 Thread Tim Golden

On 18/03/2011 16:41, JSkinn3 wrote:

I'm new to python and I am trying to figure out how to remove all sub
directories from a parent directory using a wildcard.  For example,
remove all sub directory folders that contain the word "PEMA" from the
parent directory "C:\Data".

I've trying to use os.walk with glob, but I'm not sure if this is the
right path to take.


You've got a few options. And it depends whether you just want
to get it done as simply as possible or whether you're using
this as a learning exercise.

Assuming it's somewhere between the two then you're on the right
track with os.walk:


import os
import shutil

for dirpath, dirnames, filenames in os.walk ("c:/data"):
  for dirname in dirnames:
if "pema" in dirname.lower ():
  pema_path = os.path.join (dirpath, dirname)
  print "Removing", pema_path
  shutil.rmtree (pema_path)
  dirnames.remove (dirname)



The key is that os.walk is designed so that the dirnames
list can be mutated in place to remove directories which
are to be ignored for whatever reason. In this case, you
delete the directory tree and remove it from os.walk's
attention.

BTW the use of os.walk here is only necessary if you need to
remove "*pema*" directories at any level under c:\data. If
you only need those immediately under c:\data then you can
just use glob.glob in union with shutil.rmtree

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


Re: delete namespaces

2011-03-30 Thread Tim Golden

On 30/03/2011 8:03 PM, Terry Reedy wrote:

On 3/30/2011 5:10 AM, Jean-Michel Pichavant wrote:


3/ if you want to do the 2/ but require a painful long prologue to your
test, then you may want to use the builtin reload. Use it with care,
because any existing object created from the previous module will not be
affected, they'll still hold the previous code. "reload" solves some
problems, but bring others, especially for the newcomer.


Guido removed it in 3.x because it is badly flawed and he could see any
way to sensibly fix it.


Well, "moved" rather than "removed":


Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52)
Type "help", "copyright", "credits" or "license"

import imp
imp.reload








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


Re: Get USB ID of a serial port through pyserial?

2011-03-31 Thread Tim Golden

On 30/03/2011 20:01, John Nagle wrote:

Is there some way to get the USB ID of a serial port through
pyserial on Linux and/or Windows? USB serial port devices have
device names determined by when they were plugged in. So, if
you have more than one USB serial device, you need the USB device's
built-in ID to figure out what's out there.

Is there a way to get that info portably?


I appreciate that you're after a portable solution and are
using pyserial, but since no-one's responded (publicly), then
let me at least offer a WMI solution which should work on
Windows:


import win32com.client

wmi = win32com.client.GetObject ("winmgmts:")
for usb in wmi.InstancesOf ("Win32_USBHub"):
  print usb.DeviceID



Now, that shows that WMI can "see" USB devices, but to
get from that database record to something more physical,
such as a disk drive or a COM port usually involves a
merry dance across the WMI namespace, essentially joining
to successive entity classes until you reach the one you
want.

If you think this might be worth pursuing for your case,
feel free to get back and ask.

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


Re: python2.7.1 freebsd development version

2011-04-01 Thread Tim Golden

On 01/04/2011 15:25, Tim wrote:

hi,
I can't seem to find the development version of python2.7.1; maybe
there isn't one any longer, but according to this post, there was a
bug in the configure script that affects freebsd machines; the patch
to fix it was made, but I think I'm not picking that fix up.
http://bugs.python.org/file19836/freebsd_ldshared.patch

Where should I look for the updated version?


This is the Mercurial repo:

  http://hg.python.org/cpython/

Look for the 2.7 named branch which tracks 2.7.x development

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


Re: python2.7.1 freebsd development version

2011-04-01 Thread Tim Golden

On 01/04/2011 16:34, Tim wrote:

On Apr 1, 10:55 am, Tim Golden  wrote:

On 01/04/2011 15:25, Tim wrote:


hi,
I can't seem to find the development version of python2.7.1; maybe
there isn't one any longer, but according to this post, there was a
bug in the configure script that affects freebsd machines; the patch
to fix it was made, but I think I'm not picking that fix up.
http://bugs.python.org/file19836/freebsd_ldshared.patch



Where should I look for the updated version?


This is the Mercurial repo:

http://hg.python.org/cpython/

Look for the 2.7 named branch which tracks 2.7.x development

TJG


hi Tim, thanks -- just what I was looking for, just couldn't find it
on my own at all!


This is probably the best starting point (recently published):

  http://docs.python.org/devguide/

(first hit for: Python development)

Look down that page is "Source code"

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


Re: split string but ignore sep inside double quotes

2011-04-13 Thread Tim Golden

On 13/04/2011 15:59, Jonno wrote:

I have the following unicode object:
u'3,"Some, text",more text'

and I want to split it into a list like this:
[3,"Some, text", more text]

In other words I want to split on the comma but not if it's inside a
double-quote.


You want the csv module which is designed for *exactly* this
form of data.

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


Re: strange use of %s

2011-04-18 Thread Tim Golden

On 18/04/2011 09:29, Tracubik wrote:

Hi all,
i'm reading a python tutorial in Ubuntu's Full Circle Magazine and i've
found this strange use of %s:

sql = "SELECT pkid,name,source,servings FROM Recipes WHERE name like '%%%s%
%'" %response

response is a string. I've newbie in sql.

why do the coder use %%%s%% instead of a simple %s?
why he also use the ''?


Two parts to this answer.

The straightforward one: because the SQL string needs to end
up looking like this: "... WHERE name LIKE '%abcd%'" and
since it's being generated by Python's string substitution,
the surrounding percents need to be doubled up in the original
string to be left as single in the final string.

An alternative in a modern Python might be to use string formatting:
"... WHERE name LIKE '%{}%'".format (response)

HOWEVER... this is not the best way to introduce Python values into
a SQL string. It's better to use the db module's string substitution
flag (often ? or :field or, confusingly, %s). This is because the
approach above lends itself to what's called SQL injection.
Obligatory xkcd reference: http://xkcd.com/327/

The code would be better if written something like this:

  sql = "SELECT ... WHERE name LIKE '%' + ? + '%'"
  q = db.cursor ()
  q.execute (sql, [response])

(The details will vary according to the database being used etc.)

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


Re: Cannot get past this string related issue

2011-04-26 Thread Tim Golden

On 26/04/2011 14:48, Oltmans wrote:

Greetings, I hope you're doing well. I'm stuck in a strange issue,
most likely due to my own ignorance. I'm reading a config file using
ConfigParser module and passing database related info to _mssql.


[ ... ]


Config file looks like following

[DB_INFO]
server = "server"
database = "database"
user = "user"
password = "password"



A config file isn't a Python file: you don't need (and
don't want) double-quotes around those values.

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


  1   2   3   4   5   6   7   8   9   10   >