Re: where to view open() function's C implementation source code ?

2012-12-18 Thread Terry Reedy

On 12/18/2012 12:25 AM, iMath wrote:

where to view  open() function's C implementation source code ?


depends on the python version. io in 3.x
otherwise, it is a builtin

--
Terry Jan Reedy


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


Is it possible monkey patch like this?

2012-12-18 Thread Marc Aymerich
Dear all,
I want to monkey patch a method that has lots of code so I want to avoid 
copying all the original method for changing just two lines. The thing is that 
I don't know how to do this kind of monkey patching.

Consider the following code:

class OringinalClass(object):
def origina_method(self, *args, **kwargs):
...
if some_condition(): # This condition should be changed
raise SomeException
...
if some_condition():
...
#if some_condition(local_variable): # This condition should be added
#raise SomeException 
...


Is it possible to tell Python to run the original method without stopping when 
an exception is raised? so I can catch them on a wrapper method and apply my 
conditional there.

Any other idea on how to monkey patch those two conditionals ?


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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Hans Mulder
On 18/12/12 06:10:43, photonym...@gmail.com wrote:
> I hope I understand the question... but shouldn't you wait for the process to 
> complete before exiting?
> 
> Something like:
> 
> pid = subprocess.Popen(...)
> pid.wait()
> 
> Otherwise, it'll exit before the background process is done. 

Why would that be a problem?

-- HansM

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


Re: Delete dict and subdict items of some name

2012-12-18 Thread Hans Mulder
On 18/12/12 06:30:48, Gnarlodious wrote:
> This problem is solved, I am so proud of myself for figuring it out!
> After reading some of these ideas I discovered the plist is really
> lists underneath any "Children" key:
> 
> 
> from plistlib import readPlist
> 
> def explicate(listDicts):
>   for dict in listDicts:
>   if 'FavIcon' in dict:
>   del dict['FavIcon']
>   if 'Children' in dict:
>   dict['Children']=explicate(dict['Children'])
>   return listDicts

It would be more Pythonic to return None, to indicate that you've
changed the list in situ.

Since None is the default return value, this means you can leave
out the return statement.


Hope this helps,

-- HansM

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Dave Angel
On 12/18/2012 05:27 AM, Hans Mulder wrote:
> On 18/12/12 06:10:43, photonym...@gmail.com wrote:
>> I hope I understand the question... but shouldn't you wait for the process 
>> to complete before exiting?
>>
>> Something like:
>>
>> pid = subprocess.Popen(...)
>> pid.wait()
>>
>> Otherwise, it'll exit before the background process is done. 
> Why would that be a problem?
>

Because you don't want to bog the system down with a zombie task.



-- 

DaveA

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


Re: Is it possible monkey patch like this?

2012-12-18 Thread Chris Angelico
On Tue, Dec 18, 2012 at 9:26 PM, Marc Aymerich  wrote:
> Any other idea on how to monkey patch those two conditionals ?

Would it be plausible to simply add a parameter to the function that
controls its behaviour? It'd be a lot more readable than fiddling from
the outside ever would.

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


I need help with graphs in python (2.7.3.1)

2012-12-18 Thread hevymetl07
Hi there, I hope that there is someone willing to help me out, I need to 
generate a graph in Python 2.7.3.1, but I am not that skilled with Python and I 
am completely stuck :(

I had to make a .CSV file from my Windows system logs (already did that), and 
the following steps must bw written in the form of a Python 2.7.3.1 script.

These are some steps I need to do first before creating the graph:
- Get the name of a CSV file from an ini file,
- Next, read a line from this CSV file,
- Increase a variable depending on the Log level (information, warning, error, 
critical),
- Read the next line,
- Then I need to print these results in a Graph.

I really hope that there is someone there to help me out, and many thanks in 
advance for all your help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.system and subprocess odd behavior

2012-12-18 Thread Hans Mulder
On 18/12/12 11:39:56, Dave Angel wrote:
> On 12/18/2012 05:27 AM, Hans Mulder wrote:
>> On 18/12/12 06:10:43, photonym...@gmail.com wrote:
>>> I hope I understand the question... but shouldn't you wait for the process 
>>> to complete before exiting?
>>>
>>> Something like:
>>>
>>> pid = subprocess.Popen(...)
>>> pid.wait()
>>>
>>> Otherwise, it'll exit before the background process is done. 
>> Why would that be a problem?
>>
> 
> Because you don't want to bog the system down with a zombie task.

I think you're confusing zombies with orphans.  A zombie results when
a child exits before the parent and the parent fails to wait for it.
An orphan is what you get if the parent exits before the child does.

On a Un*x system, the 'init' process (the process with ID 1) inherits
orphan processes and collects their exit status when they terminate.
Thus orphans do not become zombies.

I have no idea what would happen under Windows.


Hope this helps,

-- HansM

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


Re: I need help with graphs in python (2.7.3.1)

2012-12-18 Thread Thomas Bach
Hi,

Most of the tasks you have to do can be achieved quite easily with the
standard library (except the plotting).

On Tue, Dec 18, 2012 at 03:29:49AM -0800, hevymet...@gmail.com wrote:
> These are some steps I need to do first before creating the graph:
> - Get the name of a CSV file from an ini file,
Have a look at the ConfigParser module[1],

> - Next, read a line from this CSV file,
well, the csv module[2] apparently is a good choice for that,

> - Increase a variable depending on the Log level (information,
> - warning, error, critical),
you maybe want to use collections.counter for that[3]. Anyways, I
think matplotlib can handle the counting for you…

> - Read the next line,
this is just iteration over the csv instance you created with your file

> - Then I need to print these results in a Graph.
I use matplotlib for this purpose, have a look at their gallery[4] to
decide what kind of plot best fits your needs.


I'd recommend to install ipython and start playing around with the
modules I just told you. If you are stuck somewhere read the
documentation properly (it's actually all in there) and come back
again if you cannot come up with a solution.


Hope this helps,
 Thomas Bach.

Footnotes: 
[1]  http://docs.python.org/2/library/configparser.html

[2]  http://docs.python.org/2/library/csv.html

[3]  http://docs.python.org/2/library/collections.html#collections.Counter

[4]  http://matplotlib.org/gallery.html

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


Re: Is it possible monkey patch like this?

2012-12-18 Thread Peter Otten
Marc Aymerich wrote:

> Dear all,
> I want to monkey patch a method that has lots of code so I want to avoid
> copying all the original method for changing just two lines. The thing is
> that I don't know how to do this kind of monkey patching.
> 
> Consider the following code:
> 
> class OringinalClass(object):
> def origina_method(self, *args, **kwargs):
> ...
> if some_condition(): # This condition should be changed
> raise SomeException
> ...
> if some_condition():
> ...
> #if some_condition(local_variable): # This condition should be
> #added
> #raise SomeException
> ...
> 
> 
> Is it possible to tell Python to run the original method without stopping
> when an exception is raised? 

No.

> so I can catch them on a wrapper method and
> apply my conditional there.
> 
> Any other idea on how to monkey patch those two conditionals ?

One of the cleanest alternatives is to factor out the condition in the 
original class and then use a subclass:

class Original:
def method(self, *args, **kw):
self.check_condition(...)
...
def check_condition(self, ...):
if condition:
raise SomeException

class Sub(Original):
def check_condition(self, ...):
pass

If you insist on monkey-patching possible solutions depend on the actual 
conditions. If some_condition() is a function, replace that function. If it 
is actually an expression tweak the arguments. E. g:

>>> class Original:
... def method(self, x):
... if x < 0: raise ValueError
... print x * x
... 
>>> Original().method(-2)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in method
ValueError
>>> class Int(int):
... def __lt__(self, other): return False # a blunt lie
... 
>>> Original().method(Int(-2))
4

This tends to get complex quickly, so in the long run you will not be happy 
with that approach...

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


[newbie] problem making equally spaced value array with linspace

2012-12-18 Thread Jean Dubois
I have trouble with the code beneath to make an array with equally
spaced values
When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
get the following result:
[ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
But I was hoping for:
[ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
then again for 0.01,0.07,0.01

What I find strange is that for the 1st example "1+abs(float(endvalue)-
float(startvalue))/float(incr)" gives 7.0 but int() of this value
gives 6
can someone provide help with this issue?
thanks
jean

#!/usr/bin/python
import math
import numpy as np
print "Enter start value as a float (e.g. 0.001) or in scientific
notation (e.g. 1e-3): ",
startvalue = raw_input()
print "Enter end value: ",
endvalue = raw_input()
print "Enter step: ",
incr = raw_input()
#nom = number of measurements
nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr))
array=np.linspace(float(startvalue), float(endvalue), float(nom))
print "Array with current values: ",array
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [newbie] problem making equally spaced value array with linspace

2012-12-18 Thread Peter Otten
Jean Dubois wrote:

> I have trouble with the code beneath to make an array with equally
> spaced values
> When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
> get the following result:
> [ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
> But I was hoping for:
> [ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
> It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
> then again for 0.01,0.07,0.01
> 
> What I find strange is that for the 1st example "1+abs(float(endvalue)-
> float(startvalue))/float(incr)" gives 7.0 but int() of this value
> gives 6
> can someone provide help with this issue?
> thanks
> jean
> 
> #!/usr/bin/python
> import math
> import numpy as np
> print "Enter start value as a float (e.g. 0.001) or in scientific
> notation (e.g. 1e-3): ",
> startvalue = raw_input()
> print "Enter end value: ",
> endvalue = raw_input()
> print "Enter step: ",
> incr = raw_input()
> #nom = number of measurements
> nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr))
> array=np.linspace(float(startvalue), float(endvalue), float(nom))
> print "Array with current values: ",array

If you repeat the calculation of the number of intervals in the interpreter 
you get

>>> 1 + abs(0.0007-0.0001)/0.0001
6.999

Many numbers cannot be represented exactly as float (that's the price you 
have to pay for covering a wide range with just a few (8) bytes), and you 
have introduced such a small error. The subsequent int() call will round 
that float to the integer below it:

>>> int(_)
6


While applying round() would work here

>>> int(round(1 + abs(0.0007-0.0001)/0.0001))
7

there is no once-and-for-all solution to the underlying problem. E. g. 

>>> x = 2.**53
>>> x == x + 1
True


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


Re: [newbie] problem making equally spaced value array with linspace

2012-12-18 Thread Alexander Blinne
Am 18.12.2012 13:37, schrieb Jean Dubois:
> I have trouble with the code beneath to make an array with equally
> spaced values
> When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
> get the following result:
> [ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
> But I was hoping for:
> [ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
> It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
> then again for 0.01,0.07,0.01
> 
> What I find strange is that for the 1st example "1+abs(float(endvalue)-
> float(startvalue))/float(incr)" gives 7.0 but int() of this value
> gives 6
> can someone provide help with this issue?
> thanks
> jean
> 
> #!/usr/bin/python
> import math
> import numpy as np
> print "Enter start value as a float (e.g. 0.001) or in scientific
> notation (e.g. 1e-3): ",
> startvalue = raw_input()
> print "Enter end value: ",
> endvalue = raw_input()
> print "Enter step: ",
> incr = raw_input()
> #nom = number of measurements
> nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr))
> array=np.linspace(float(startvalue), float(endvalue), float(nom))
> print "Array with current values: ",array

The Problem is the accuracy/precision of floating point operations

Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 100e-6 #start
>>> b = 700e-6 #end
>>> c = 100e-6 #incr
>>> 1+(b-a)/c
6.999

and the fact that int() only takes the integer part of a floating point
number.

>>> int(1+(b-a)/c)
6

So you have to make a more detailed decision about the number of points
in the case that (end-start)/incr is not exactly an integer which it
will almost never be.

The np.arange(a,b,c) function chooses a simple rule: give a list of
numbers a + k * c with k running from 0 to the highest integer with a +
k * c < b.

>>> np.arange(a,b,c)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006])

You can get your desired list by adding some epsilon to the value of b.
Just make sure your epsilon is quite small compared to c.

>>> np.arange(a,b+1e-15,c)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006,  0.0007])

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


Re: problem making equally spaced value array with linspace

2012-12-18 Thread Jean Dubois
On 18 dec, 14:09, Peter Otten <__pete...@web.de> wrote:
> Jean Dubois wrote:
> > I have trouble with the code beneath to make an array with equally
> > spaced values
> > When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
> > get the following result:
> > [ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
> > But I was hoping for:
> > [ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
> > It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
> > then again for 0.01,0.07,0.01
>
> > What I find strange is that for the 1st example "1+abs(float(endvalue)-
> > float(startvalue))/float(incr)" gives 7.0 but int() of this value
> > gives 6
> > can someone provide help with this issue?
> > thanks
> > jean
>
> > #!/usr/bin/python
> > import math
> > import numpy as np
> > print "Enter start value as a float (e.g. 0.001) or in scientific
> > notation (e.g. 1e-3): ",
> > startvalue = raw_input()
> > print "Enter end value: ",
> > endvalue = raw_input()
> > print "Enter step: ",
> > incr = raw_input()
> > #nom = number of measurements
> > nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr))
> > array=np.linspace(float(startvalue), float(endvalue), float(nom))
> > print "Array with current values: ",array
>
> If you repeat the calculation of the number of intervals in the interpreter
> you get
>
> >>> 1 + abs(0.0007-0.0001)/0.0001
>
> 6.999
>
> Many numbers cannot be represented exactly as float (that's the price you
> have to pay for covering a wide range with just a few (8) bytes), and you
> have introduced such a small error. The subsequent int() call will round
> that float to the integer below it:
>
> >>> int(_)
>
> 6
>
> While applying round() would work here
>
> >>> int(round(1 + abs(0.0007-0.0001)/0.0001))
>
> 7
>
> there is no once-and-for-all solution to the underlying problem. E. g.
>
> >>> x = 2.**53
> >>> x == x + 1
>
> True

thanks

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


Re: where to view open() function's C implementation source code ?

2012-12-18 Thread Roy Smith
In article ,
 iMath  wrote:

> � 2012”N12ŒŽ18“��Š�“�UTC+8‰�Œ�1时35分58秒,Roy 
> Smith写道:
> > In article ,
> > 
> >  iMath  wrote:
> > 
> > 
> > 
> > > where to view  open() function's C implementation source code ?
> > 
> > 
> > 
> > http://www.python.org/download/releases/
> > 
> > 
> > 
> > Download the source for the version you're interested in.
> 
> but which python module is  open() in ?

I met you half-way, I showed you where the source code is.  Now you 
need to come the other half and look at the code.  Maybe start by 
grepping the entire source tree for "open"?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where to view open() function's C implementation source code ?

2012-12-18 Thread Chris Angelico
On Wed, Dec 19, 2012 at 1:28 AM, Roy Smith  wrote:
> In article ,
>  iMath  wrote:
>> > Download the source for the version you're interested in.
>>
>> but which python module is  open() in ?
>
> I met you half-way, I showed you where the source code is.  Now you
> need to come the other half and look at the code.  Maybe start by
> grepping the entire source tree for "open"?

Ouch, that mightn't be very effective! With some function names, you
could do that. Not so much "open". Still, it'd be a start...

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


Re: Delete dict and subdict items of some name

2012-12-18 Thread Gnarlodious
On Tuesday, December 18, 2012 3:31:41 AM UTC-7, Hans Mulder wrote:
> On 18/12/12 06:30:48, Gnarlodious wrote:
> 
> > This problem is solved, I am so proud of myself for figuring it out!
> 
> > After reading some of these ideas I discovered the plist is really
> 
> > lists underneath any "Children" key:
> 
> > 
> 
> > 
> 
> > from plistlib import readPlist
> 
> > 
> 
> > def explicate(listDicts):
> 
> > for dict in listDicts:
> 
> > if 'FavIcon' in dict:
> 
> > del dict['FavIcon']
> 
> > if 'Children' in dict:
> 
> > dict['Children']=explicate(dict['Children'])
> 
> > return listDicts
> 

> It would be more Pythonic to return None, to indicate that you've
> changed the list in situ.
> 
> Since None is the default return value, this means you can leave
> out the return statement.
But then it only operates on the outer layer, inner layers might get processed 
but not written. Unless I don't understand what you're saying.

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


Re: [newbie] problem making equally spaced value array with linspace

2012-12-18 Thread Dave Angel
On 12/18/2012 07:37 AM, Jean Dubois wrote:
> I have trouble with the code beneath to make an array with equally
> spaced values
> When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I
> get the following result:
> [ 0.0001   0.00022  0.00034  0.00046  0.00058  0.0007 ]
> But I was hoping for:
> [ 0.0001   0.0002  0.0003  0.0004  0.0005  0.0006 0.0007]
> It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1
> then again for 0.01,0.07,0.01
I started this answer before there were any others visible, but had to
rush off to the dentist.  Now I'm finishing it and sending it, even
though there's some overlap with the other posts.

What Python has in common with nearly every other language is the use of
binary floating point.  Unlike integers, floating point values can have
both roundoff and quantization errors.  The former happen with many
operations, like you can see with pencil and paper trying to divide 1 by
3, and storing the result in 10 columns.

But quantization errors aren't as obvious.  They can occur whenever you
convert a number from one format to another.  In this case, you're
converting from the decimal string to a binary float.  To put it simply
binary floats can't store any decimal number exactly exact integers, and
mixed numbers where the fractional part happens to be an exact multiple
of a power of two.  So 0.5 is exact, but you get quantization error with
0.1, 0.2, ... 0.9

This is compounded by the fact that print will convert the number back
to a decimal string.  So sometimes the two quantization errors happen to
cancel, and sometimes they don't.

Now, your particular case is due to the convoluted way you calculate
nom.  And the easiest way to fix it would be to add 0.5 before
truncating with int().  Or better yet, have the user tell you how many
he wants in his list, and calculate the other way around.  If instead of
10 items, you have 10 of them, you'll get a cumulative error with
your approach.  So you'd use a formula like
 start + (end-start)*i/nom

which would assure that each value was really close without cumulative
errors.  No idea how that'd fit with numpy.

Another approach is to use the Decimal package.  It's not inherently any
more accurate, but the errors are where you'd expect them, and you don't
get the quantization error converting back and forth to string.


-- 

DaveA

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


Re: Delete dict and subdict items of some name

2012-12-18 Thread Terry Reedy

On 12/18/2012 10:27 AM, Gnarlodious wrote:

On Tuesday, December 18, 2012 3:31:41 AM UTC-7, Hans Mulder wrote:

On 18/12/12 06:30:48, Gnarlodious wrote:



from plistlib import readPlist


I do not see this used below.


def explicate(listDicts):
for dict in listDicts:
if 'FavIcon' in dict:
del dict['FavIcon']
if 'Children' in dict:
dict['Children']=explicate(dict['Children'])
return listDicts



It would be more Pythonic to return None, to indicate that you've
changed the list in situ.


And since it is being changed at the top level (by deletion), it should 
be changed in place all the way down.



Since None is the default return value, this means you can leave
out the return statement.


dict['Children']=explicate(dict['Children'])
would then need to be
explicate(dict['Children'])


But then it only operates on the outer layer,

> inner layers might get processed but not written.

I believe the above answers your concern. But to be sure it is correct, 
YOU NEED TEST CASES. In fact, your original post should have contained 
at least one non-trivial test case: an input dict and what you wanted it 
to look like after processing. Writing at least some tests before code 
is a great idea.


--
Terry Jan Reedy

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


Re: I need help with graphs in python (2.7.3.1)

2012-12-18 Thread Terry Reedy

On 12/18/2012 6:29 AM, hevymet...@gmail.com wrote:

Hi there, I hope that there is someone willing to help me out, I need
to generate a graph in Python 2.7.3.1,


There is only 2.7.3, no 2.7.3.1, at least not officially.


I had to make a .CSV file from my Windows system logs (already did
that), and the following steps must bw written in the form of a
Python 2.7.3.1 script.

These are some steps I need to do first before creating the graph: -
Get the name of a CSV file from an ini file, - Next, read a line from
this CSV file, - Increase a variable depending on the Log level
(information, warning, error, critical), - Read the next line, - Then
I need to print these results in a Graph.


It sounds like your data consist of 4 counts. If so, you can easily make 
a simple bar plot with characters


Log level  Number  Plot (x = 10)
-- --  -
infomation386  
warning97  xx
error   5  x
critical   23  xxx

The main problem is to compute the scaling factor from the data.
You also have to decide on the rounding (I rounded up).

--
Terry Jan Reedy

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


Re: Is it possible monkey patch like this?

2012-12-18 Thread Terry Reedy

On 12/18/2012 5:26 AM, Marc Aymerich wrote:


I want to monkey patch a method that has lots of code so I want to
avoid copying all the original method for changing just two lines.


You omitted the most important piece of information. Can you modify the 
original code (or get someone else to do so) or must you leave it as is? 
Chris and Peter gave you answers for the former case. If the latter, you 
must copy and modify for the change you specified.


--
Terry Jan Reedy

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread py_genetic
Oscar I can confirm this behavior from terminal. 

AND this works as well, simulating exactly what I'm doing permissions wise, and 
calling sudo python test.py  below

f1 = open('TESTDIR/file1.txt', 'w')
f1.write('some test here\n')
f1.close()

cmd1 = 'cat < TESTDIR/file1.txt > TESTDIR/file2.txt'
P = Popen(cmd1, shell=True)
P.wait()

cmd2 = 'cat < TESTDIR/file1.txt | sudo tee TESTDIR/file3.txt'
P = Popen(cmd2, shell=True)
P.wait()

-rw-r--r-- 1 root root   15 Dec 18 12:57 file1.txt
-rw-r--r-- 1 root root   15 Dec 18 12:57 file2.txt
-rw-r--r-- 1 root root   15 Dec 18 12:57 file3.txt

HOWEVER... 

when using this command from before no dice

/usr/local/Calpont/mysql/bin/mysql 
--defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < 
/home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT

OR

/usr/local/Calpont/mysql/bin/mysql 
--defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < 
/home/myusr/jobs/APP_JOBS/JOB_XXX.SQL | sudo tee 
/home/myusr/jobs/APP_JOBS/JOB_XXX.TXT

So it's basically as if python gets a response instantly (perhaps from the 
query) and closes the process, since we've verified its not permissions related.

Perhaps someone can try a mysql cmd line such as above within python?  And see 
if you can verify this behavior.  I believe the query returning with no errors 
is shutting the sub shell/process?

I've tried this with all options p.wait() ect as well as parsing the command 
and running shell false.

Again the exact command run perfect when pasted and run from the shell.  I'll 
try running it a few other ways with some diff db options.


> Follow through the bash session below
> 
> 
> 
> $ cd /usr
> 
> $ ls
> 
> bin  games  include  lib  local  sbin  share  src
> 
> $ touch file
> 
> touch: cannot touch `file': Permission denied
> 
> $ sudo touch file
> 
> [sudo] password for oscar:
> 
> $ ls
> 
> bin  file  games  include  lib  local  sbin  share  src
> 
> $ cat < file > file2
> 
> bash: file2: Permission denied
> 
> $ sudo cat < file > file2
> 
> bash: file2: Permission denied
> 
> $ sudo cat < file > file2
> 
> bash: file2: Permission denied
> 
> $ sudo cat < file | tee file2
> 
> tee: file2: Permission denied
> 
> $ sudo cat < file | sudo tee file2
> 
> $ ls
> 
> bin  file  file2  games  include  lib  local  sbin  share  src
> 
> 
> 
> The problem is that when you do
> 
> 
> 
>   $ sudo cmd > file2
> 
> 
> 
> it is sort of like doing
> 
> 
> 
>   $ sudo cmd | this_bash_session > file2
> 
> 
> 
> so the permissions used to write to file2 are the same as the bash
> 
> session rather than the command cmd which has root permissions. By
> 
> piping my output into "sudo tee file2" I can get file2 to be written
> 
> by a process that has root permissions.
> 
> 
> 
> I suspect you have the same problem although it all complicated by the
> 
> fact that everything is a subprocess of Python. Is it possibly the
> 
> case that the main Python process does not have root permissions but
> 
> you are using it to run a command with sudo that then does have root
> 
> permissions?
> 
> 
> 
> Does piping through something like "sudo tee" help?
> 
> 
> 
> 
> 
> Oscar

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Oscar Benjamin
Can you trim content and interleave your response (instead of
top-posting) please?

On 18 December 2012 18:26, py_genetic  wrote:
> HOWEVER...
>
> when using this command from before no dice
>
> /usr/local/Calpont/mysql/bin/mysql 
> --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < 
> /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT
>
> OR
>
> /usr/local/Calpont/mysql/bin/mysql 
> --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < 
> /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL | sudo tee 
> /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT
>
> So it's basically as if python gets a response instantly (perhaps from the 
> query) and closes the process, since we've verified its not permissions 
> related.

I wouldn't say that this is verified.

Have you verified that without the last redirection (to the output
file) you can correctly read the stdout from within Python?

Does the same command work if you put it in a shell script? If so what
about making a script like so:

#!/usr/bin/env bash

jobname="$1"

/usr/local/Calpont/mysql/bin/mysql
--defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB <
"/home/myusr/jobs/APP_JOBS/JOB_${jobname}.SQL" | sudo tee
"/home/myusr/jobs/APP_JOBS/JOB_${jobname}.TXT"

If that works when you run it directly, does it work if you do:
subprocess.check_call(['myscript.sh', jobname])


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


Re: py2exe is on Sourceforge list of top growth projects

2012-12-18 Thread Colin J. Williams

On 18/12/2012 1:52 AM, Frank Millman wrote:

This is from Sourceforge's monthly update -



Top Growth Projects

We're always on the lookout for projects that might be doing interesting
things, and a surge in downloads is one of many metrics that we look at
to identify them. Here's the projects that had the greatest growth in
the last month.

[...]

py2exe: A distutils extension to create standalone Windows programs from
python scripts.



It is 19th on a list of 19, but still, it is nice to see. I wonder if
there was any particular reason for that?

Frank Millman


Yes, but py2exe appears limited to Python 2.6.

PyInstaller is another option with similar functionality.

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


Re: where to view open() function's C implementation source code ?

2012-12-18 Thread Andrew Robinson

On 12/18/2012 07:03 AM, Chris Angelico wrote:

On Wed, Dec 19, 2012 at 1:28 AM, Roy Smith  wrote:

In article,
  iMath  wrote:

Download the source for the version you're interested in.

but which python module is  open() in ?

I met you half-way, I showed you where the source code is.  Now you
need to come the other half and look at the code.  Maybe start by
grepping the entire source tree for "open"?

Ouch, that mightn't be very effective! With some function names, you
could do that. Not so much "open". Still, it'd be a start...

ChrisA
In Python3.3.0 -- the built in open() appears in 
Python-3.3.0/Modules/_io/_iomodule.c;
There is another module defined in an object in 
Python-3.3.0/Modules/_io/fileio.c; but I don't think that the one called 
when a lone x=open(...) is done.


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


Re: py2exe is on Sourceforge list of top growth projects

2012-12-18 Thread Vlastimil Brom
2012/12/18 Colin J. Williams :
...
> Yes, but py2exe appears limited to Python 2.6.
>
> PyInstaller is another option with similar functionality.
>
> Colin W.
> --
> http://mail.python.org/mailman/listinfo/python-list

There are versions for python 2.3 - 2.7:
http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/
(for some reason, 2.6 appears in a more visible link)

However, unfortunately, there isn't any support for python 3 (that I
know of - I'd love to be wrong in this).

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread py_genetic
Solved the issue, by injecting the query into the cmd line.  Shell script 
worked fine as if I was cutting and pasting to the prompt.  Seems to still be 
something with the subprocess receiving and exit code before or when the query 
finishes, just when I ask to to read from the .SQL file.

example called from in python:
mysql  < file.txt > out.txt  < doesn't work (query is run 0Byte output)
mysql  -e "my query" > out.txt <- does work

However this isn't standard mysql as it's infinidb.  Maybe this is an esoteric 
issue.

Thanks for the help Oscar.  Frustrating since it seems illogical seems if 
the cmd runs in the shell it should have the exact same behavior from a 
subprocess shell=True cmd string call.

If I find anything else I'll update this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where to view open() function's C implementation source code �

2012-12-18 Thread rurpy
On Monday, December 17, 2012 10:35:58 PM UTC-7, Roy Smith wrote:
> iMath  wrote:
> > where to view  open() function's C implementation source code ?
> http://www.python.org/download/releases/
> Download the source for the version you're interested in.

iMath:

There is no need to download the source.  You can browse the 
source code online.  For the v3.0.0 version of open():
  hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c

For 2.7.3 I think what you want is the  builtin_open() function in 
  http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c
and the file object and open_the_file() function in
 http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c

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


Re: where to view open() function's C implementation source code �

2012-12-18 Thread rurpy
On Tuesday, December 18, 2012 1:57:37 PM UTC-7, ru...@yahoo.com wrote:
>[...] 
> source code online.  For the v3.0.0 version of open():
>   hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c

oops, that should have been:
  http://hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c
-- 
http://mail.python.org/mailman/listinfo/python-list


Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Tom Borkin
Hi;
I have this test code:

if i_id == "1186":
  sql = 'insert into interactions values(Null, %s, "Call Back", "%s")'
% (i_id, date_plus_2)
  cursor.execute(sql)
  db.commit()
  print sql
It prints the sql statement, but it doesn't execute. If I copy and paste
the sql into the mysql command line it does execute without warnings or
errors. What gives?
TIA,
Tom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Wayne Werner

On Tue, 18 Dec 2012, Tom Borkin wrote:


Hi;
I have this test code:
 
    if i_id == "1186":
  sql = 'insert into interactions values(Null, %s, "Call Back", "%s")' % 
(i_id, date_plus_2)
  cursor.execute(sql)
  db.commit()
  print sql
It prints the sql statement, but it doesn't execute. If I copy and paste the 
sql into the mysql command line it does execute without warnings or errors. 
What gives?


Does date_plus_2 contain

 "Robert"); DROP TABLE interactions; --

By any chance?
-W-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Tom Borkin
No (lol). It returns a date as a string: "2012-12-12" for example.
Tom


On Tue, Dec 18, 2012 at 6:02 PM, Wayne Werner  wrote:

> On Tue, 18 Dec 2012, Tom Borkin wrote:
>
>  Hi;
>> I have this test code:
>>
>> if i_id == "1186":
>>   sql = 'insert into interactions values(Null, %s, "Call Back",
>> "%s")' % (i_id, date_plus_2)
>>   cursor.execute(sql)
>>   db.commit()
>>   print sql
>> It prints the sql statement, but it doesn't execute. If I copy and paste
>> the sql into the mysql command line it does execute without warnings or
>> errors. What gives?
>>
>
> Does date_plus_2 contain
>
>  "Robert"); DROP TABLE interactions; --
>
> By any chance?
> -W
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Chris Angelico
On Wed, Dec 19, 2012 at 9:28 AM, Tom Borkin  wrote:
> No (lol). It returns a date as a string: "2012-12-12" for example.
> Tom

Then that's why it doesn't work. Wayne was hinting at a major MAJOR
problem with your code; it's interpolating data into the SQL
statement, instead of providing parameters to the query.

Don't do it! Just don't!

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Hans Mulder
On 17/12/12 21:56:50, py_genetic wrote:
> /usr/local/Calpont/mysql/bin/mysql 
> --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < 
> /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT

If you're trying to interact with a MySQL database, then
you should really use the myslqdb module.  Trying to parse
the output of the command-line utility is harder, and is
too fragile for production use.


Hope this helps,

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


Re: os.system and subprocess odd behavior

2012-12-18 Thread Cameron Simpson
On 18Dec2012 05:39, Dave Angel  wrote:
| On 12/18/2012 05:27 AM, Hans Mulder wrote:
| > On 18/12/12 06:10:43, photonym...@gmail.com wrote:
| >> I hope I understand the question... but shouldn't you wait for the process 
to complete before exiting?
| >>
| >> Something like:
| >>
| >> pid = subprocess.Popen(...)
| >> pid.wait()
| >>
| >> Otherwise, it'll exit before the background process is done. 
| > Why would that be a problem?
| 
| Because you don't want to bog the system down with a zombie task.

A zombie task is just a process table slot; it costs the system almost
nothing. It is untidy but except in extreme cases, not a performance or
resource issue.

OTOH, a child process that is still active (pointlessly) might be a
problem...
-- 
Cameron Simpson 

My initial work-around is to rebuild history.
- g...@sci34hub.sci.com (Gary Heston)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Hans Mulder
On 18/12/12 22:34:08, Tom Borkin wrote:
> Hi;
> I have this test code:
>  
> if i_id == "1186":
>   sql = 'insert into interactions values(Null, %s, "Call Back",
> "%s")' % (i_id, date_plus_2)
>   cursor.execute(sql)
>   db.commit()
>   print sql
> It prints the sql statement, but it doesn't execute. If I copy and paste
> the sql into the mysql command line it does execute without warnings or
> errors. What gives?

What happens if you do:


if i_id == "1186":
  sql = 'insert into interactions values(Null, %s, "Call Back", %s)'
  cursor.execute(sql, (i_id, date_plus_2))
  db.commit()
  print sql

Note the absence of quotes around the second %s in the sql command.

This should work correctly even if date_plus_2 happens to contain

 Robert"); DROP TABLE interactions; --


For background information, see http://bobby-tables.com/python.html


Hope this helps,

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


Re: where to view open() function's C implementation source code �

2012-12-18 Thread iMath
在 2012年12月19日星期三UTC+8上午4时57分37秒,ru...@yahoo.com写道:
> On Monday, December 17, 2012 10:35:58 PM UTC-7, Roy Smith wrote:
> 
> > iMath  wrote:
> 
> > > where to view  open() function's C implementation source code ?
> 
> > http://www.python.org/download/releases/
> 
> > Download the source for the version you're interested in.
> 
> 
> 
> iMath:
> 
> 
> 
> There is no need to download the source.  You can browse the 
> 
> source code online.  For the v3.0.0 version of open():
> 
>   hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c
> 
> 
> 
> For 2.7.3 I think what you want is the  builtin_open() function in 
> 
>   http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c
> 
> and the file object and open_the_file() function in
> 
>  http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c
> 
> 
> 
> Hope this helps.

thanks very much !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where to view open() function's C implementation source code �

2012-12-18 Thread iMath
在 2012年12月19日星期三UTC+8上午4时57分37秒,ru...@yahoo.com写道:
> On Monday, December 17, 2012 10:35:58 PM UTC-7, Roy Smith wrote:
> 
> > iMath  wrote:
> 
> > > where to view  open() function's C implementation source code ?
> 
> > http://www.python.org/download/releases/
> 
> > Download the source for the version you're interested in.
> 
> 
> 
> iMath:
> 
> 
> 
> There is no need to download the source.  You can browse the 
> 
> source code online.  For the v3.0.0 version of open():
> 
>   hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c
> 
> 
> 
> For 2.7.3 I think what you want is the  builtin_open() function in 
> 
>   http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c
> 
> and the file object and open_the_file() function in
> 
>  http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c
> 
> 
> 
> Hope this helps.

thanks very much !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Tom Borkin
Actually, what I originally had was:
cursor.execute("""insert into interactions values(Null, %s, "Call Back",
%s)""", (i_id, date_plus_2))
and that didn't work, either. I tried your variation like:
cursor.execute("""insert into interactions values(Null, %s, "Call Back",
%s)""" % (i_id, date_plus_2))
and no cigar :(
Tom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible monkey patch like this?

2012-12-18 Thread Steven D'Aprano
On Tue, 18 Dec 2012 02:26:42 -0800, Marc Aymerich wrote:

> Dear all,
> I want to monkey patch a method that has lots of code so I want to avoid
> copying all the original method for changing just two lines. The thing
> is that I don't know how to do this kind of monkey patching.

The only types of monkey-patching supported by Python are overriding or 
overloading. In the first, you override a function by replacing it with a 
new function of your own design; in the second, you save the original 
somewhere, then replace it with a new function that wraps the old:

# overloading the len() function
_len = len
def len(obj):
print "calling len on object %r" % obj
print "returning result as a string"
return str(_len(obj))


You cannot monkey-patch in the middle of a function.

Technically, you could hack the byte code of the function, but such a 
thing is not supported or documented anywhere. If you want to learn more, 
you can google on "python byte code hacks", but let me warn you that this 
is advanced, and dark, territory where you will get little or no help 
when things go wrong.


> Consider the following code:
> 
> class OringinalClass(object):
> def origina_method(self, *args, **kwargs):
> ...
> if some_condition(): # This condition should be changed
> raise SomeException

If some_condition is a function or method call, you can patch the 
function or method. That may require subclassing another object. E.g.

if args[2].startswith('spam'): ... # CHANGE ME

instead of passing a string as args[2], you could subclass string to 
patch startswith, then pass a subclass instance instead of a string:

result = instance.original_method(x, y, 'spam', z)

becomes:

result = instance.original_method(x, y, MyStr('spam'), z)

You could even patch original_method to automatically MyStr-ify args[2].

But in general, if the condition is an arbitrary expression:

if x < y or z > 2 and spam is not None: # CHANGE ME

there is nothing you can do except replace the entire method.

> if some_condition():
> ...
> #if some_condition(local_variable): # This condition should
> be added #raise SomeException
> ...

Pretty much the same applies. If you can move the new condition to the 
start or end of the method, you can patch. To insert it in an arbitrary 
spot in the middle, forget it.

> Is it possible to tell Python to run the original method without
> stopping when an exception is raised? so I can catch them on a wrapper
> method and apply my conditional there.

No. Your request doesn't even make sense. What are you going to catch if 
no exception is raised? What do you expect the method to do if it doesn't 
stop? Consider:

def original(self, x, y):
z = x + y
print z
return "hello world"[z]


Suppose you pass x=1, y={} so that x+y fails. What do you expect Python 
to do if you tell it not to stop at an exception? What will it print? 
What result should it return?



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


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread John Gordon
In  Tom Borkin 
 writes:

> Actually, what I originally had was:
> cursor.execute("""insert into interactions values(Null, %s, "Call Back",
> %s)""", (i_id, date_plus_2))
> and that didn't work, either. I tried your variation like:
> cursor.execute("""insert into interactions values(Null, %s, "Call Back",
> %s)""" % (i_id, date_plus_2))
> and no cigar :(
> Tom

Have you tried using single-quotes around Call Back, instead of
double quotes?  I've noticed that SQL statements prefer single-quoted
strings (although that may be Oracle specific, as that's all I've really
worked with).

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: where to view open() function's C implementation source code �

2012-12-18 Thread rurpy
On 12/18/2012 04:55 PM, iMath wrote:
> > 在 2012年12月19日星期三UTC+8上午4时57分37秒,ru...@yahoo.com写道:
> >[...]
>> >> There is no need to download the source.  You can browse the 
>> >> source code online.  For the v3.0.0 version of open():
>> >>   hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c
>> >> 
>> >> For 2.7.3 I think what you want is the  builtin_open() function in 
>> >>   http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c
>> >> and the file object and open_the_file() function in
>> >>  http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c
>> >> 
>> >> Hope this helps.
> >
> > thanks very much !

Your welcome.

I noticed that you, like me, are using Google Groups.  You
might want to read this page about posting from Google Groups:

  http://wiki.python.org/moin/GoogleGroupsPython

It will help you avoid double posts and other Google Groups
posting problems that annoy other people here.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Doesn't This MySQL Statement Execute?

2012-12-18 Thread Chris Angelico
On Wed, Dec 19, 2012 at 2:57 PM, John Gordon  wrote:
> In  Tom Borkin 
>  writes:
>
>> Actually, what I originally had was:
>> cursor.execute("""insert into interactions values(Null, %s, "Call Back",
>> %s)""", (i_id, date_plus_2))
>> and that didn't work, either. I tried your variation like:
>> cursor.execute("""insert into interactions values(Null, %s, "Call Back",
>> %s)""" % (i_id, date_plus_2))
>> and no cigar :(
>> Tom
>
> Have you tried using single-quotes around Call Back, instead of
> double quotes?  I've noticed that SQL statements prefer single-quoted
> strings (although that may be Oracle specific, as that's all I've really
> worked with).

The SQL standard specifies single quotes, but MySQL and the SQL
standard aren't always on speaking terms. It depends on the MySQL
settings as to whether "asdf" means 'asdf' or means a column named
asdf.

But if that's what the problem is, there ought to be an exception
coming back, surely? I'm not familiar with the Python MySQL bindings,
but that's what I would expect.  What, specifically, does "no cigar"
mean? It executes without errors but does nothing? It purchases a gun,
aims at your shoe, and pulls the trigger?

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