Re: Getting module path string from a class instance

2012-11-12 Thread Some Developer

On 13/11/2012 07:19, Steven D'Aprano wrote:

On Tue, 13 Nov 2012 06:38:31 +, Some Developer wrote:


I'm trying to find a way to get a string of the module path of a class.

So for instance say I have class Foo and it is in a module called
my.module. I want to be able to get a string that is equal to this:
"my.module.Foo". I'm aware of the __repr__ method but it does not do
what I want it to do in this case.

Can anyone offer any advice at all?

py> from multiprocessing.pool import Pool
py> repr(Pool)
""

Seems pretty close to what you ask for. You can either pull that string
apart:

py> s = repr(Pool)
py> start = s.find("'")
py> end = s.rfind("'")
py> s[start+1:end]
'multiprocessing.pool.Pool'

or you can construct it yourself:

py> Pool.__module__ + '.' + Pool.__name__
'multiprocessing.pool.Pool'


Yeah I considered doing it this way but was wary of that method because 
of possible changes to the implementation of the __repr__ method in the 
upstream code. If the Django developers don't consider the __repr__ 
method a public API then it could change in the future breaking my code.


Of course this might not happen but I was hoping that there was a more 
generic way of doing it that did not rely on a certain implementation 
being in existence.

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


Re: Getting module path string from a class instance

2012-11-12 Thread Steven D'Aprano
On Tue, 13 Nov 2012 06:38:31 +, Some Developer wrote:

> I'm trying to find a way to get a string of the module path of a class.
> 
> So for instance say I have class Foo and it is in a module called
> my.module. I want to be able to get a string that is equal to this:
> "my.module.Foo". I'm aware of the __repr__ method but it does not do
> what I want it to do in this case.
> 
> Can anyone offer any advice at all?

py> from multiprocessing.pool import Pool
py> repr(Pool)
""

Seems pretty close to what you ask for. You can either pull that string 
apart:

py> s = repr(Pool)
py> start = s.find("'")
py> end = s.rfind("'")
py> s[start+1:end]
'multiprocessing.pool.Pool'

or you can construct it yourself:

py> Pool.__module__ + '.' + Pool.__name__
'multiprocessing.pool.Pool'


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


Re: Strange object identity problem

2012-11-12 Thread F.R.

On 11/12/2012 06:02 PM, duncan smith wrote:

On 12/11/12 13:40, F.R. wrote:

On 11/12/2012 02:27 PM, Robert Franke wrote:

Hi Frederic,

[...]


bas = {}
for year in range (2010, 2013):

 ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year)
 ba.run ()
print year, id (ba)
 bas [year] = ba

2010 150289932
2011 150835852
2012 149727788


for y in sorted (bas.keys ()):

 b = bas [year]

Shouldn't that be b = bas[y]?

Yes, it should, indeed! What's more, I should have closed and restarted
IDLE. There must have
been a name clash somewhere in the name space. The problem no longer
exists. Sorry
about that. And thanks to all who paused to reflect on this non-problem.
- Frederic.





 print y, id (b)

2010 149727788
2011 149727788
2012 149727788


[...]

Cheers,

Robert





The problem was that year was bound to the integer 2013 from the first 
loop. When you subsequently looped over the keys you printed each key 
followed by id(bas[2013]). Restarting IDLE only helped because you 
presumably didn't repeat the error.


Duncan

That's it! Isn't it strange how on occasion one doesn't see the most 
obvious and simple mistake, focusing beyond the realm of foolishness. 
Thanks all . . .


Frederic

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


Getting module path string from a class instance

2012-11-12 Thread Some Developer

I'm trying to find a way to get a string of the module path of a class.

So for instance say I have class Foo and it is in a module called 
my.module. I want to be able to get a string that is equal to this: 
"my.module.Foo". I'm aware of the __repr__ method but it does not do 
what I want it to do in this case.


Can anyone offer any advice at all?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Question regarding running .py program

2012-11-12 Thread Terry Reedy

On 11/12/2012 9:45 PM, Caroline Hou wrote:


Also, how could I edit my script? I have sth called "IDLE" installed
along with python. Is it the right place to write/edit my script?


IDLE is one way to edit; I use it. When you want to run, hit F5 and 
stdout and stderr output goes to the shell window.


--
Terry Jan Reedy

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


Re: stackoverflow quote on Python

2012-11-12 Thread Steven D'Aprano
On Tue, 13 Nov 2012 03:08:54 +, Mark Lawrence wrote:

> http://stackoverflow.com/questions/tagged/python
> 
> "Python has two major versions (2 and 3) in use which have significant
> differences."
> 
> I believe that this is incorrect.  The warts have been removed, but
> significant differences, not in my book.  If there is agreement about
> there not being significant differences, should stackoverflow be asked
> to change their wording?

Define "significant".

If StackOverflow mean "significant like the differences between Lisp and 
Cobol", then they are clearly wrong.

On the other hand, if you are suggesting that nothing short of the 
differences between Lisp and Cobol count as significant, then I think you 
too are wrong.

There are clear differences between the two versions, and the much-talked-
about "print is now a function" is the least among them:

* major reorganisation of parts of the standard library, with many
  libraries being removed, renamed, reorganised, or added;

* file objects are completely re-implemented;

* strings are now proper text strings (Unicode), not byte strings;

* nonlocal;

* keyword-only parameters for functions;

* cannot use grouped parameters in functions, e.g. def spam(a, (b,c), d)
  no longer is allowed;

* cannot use "import *" inside a function;

* dict methods keys(), values(), items() are iterators;

* so are map, reduce, zip;

* builtins like reduce, reload have been moved to modules;

* some itertools functions are now builtins;

* sorted and list.sort no longer support comparison functions;

* comparisons between different types may raise TypeError;

* extended iterable unpacking;

* function annotations;

* dict comprehensions and set literals;

* new metaclass syntax;

* classic classes are gone;

* automatic delegation doesn't work for __dunder__ methods;

* backticks `x` gone;


among others. Are these "significant" differences? Well, maybe.



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


Re: stackoverflow quote on Python

2012-11-12 Thread Ian Kelly
On Mon, Nov 12, 2012 at 8:08 PM, Mark Lawrence  wrote:
> http://stackoverflow.com/questions/tagged/python
>
> "Python has two major versions (2 and 3) in use which have significant
> differences."
>
> I believe that this is incorrect.  The warts have been removed, but
> significant differences, not in my book.  If there is agreement about there
> not being significant differences, should stackoverflow be asked to change
> their wording?

They have differences, and those differences are significant in that
they must often be taken into account when asking or answering
questions about Python on stackoverflow.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A gnarly little python loop

2012-11-12 Thread rusi
On Nov 12, 9:09 pm, Steve Howell  wrote:
> On Nov 12, 7:21 am, rusi  wrote:
>
> > On Nov 12, 12:09 pm, rusi  wrote:> This is a classic 
> > problem -- structure clash of parallel loops
>
> > 
>
> > Sorry wrong solution :D
>
> > The fidgetiness is entirely due to python not allowing C-style loops
> > like these:
>
> > >> while ((c=getchar()!= EOF) { ... }
> > [...]
>
> There are actually three fidgety things going on:
>
>  1. The API is 1-based instead of 0-based.
>  2. You don't know the number of pages in advance.
>  3. You want to process tweets, not pages of tweets.
>
> Here's yet another take on the problem:
>
>     # wrap fidgety 1-based api
>     def search(i):
>         return api.GetSearch("foo", i+1)
>
>     paged_tweets = (search(i) for i in count())
>
>     # handle sentinel
>     paged_tweets = iter(paged_tweets.next, [])
>
>     # flatten pages
>     tweets = chain.from_iterable(paged_tweets)
>     for tweet in tweets:
>         process(tweet)

[Steve Howell]
Nice on the whole -- thanks
Could not the 1-based-ness be dealt with by using count(1)?
ie use
paged_tweets = (api.GetSearch("foo", i) for i in count(1))

{Peter]
> >>> while ((c=getchar()!= EOF) { ... }

for c in iter(getchar, EOF):
...

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


Re: stackoverflow quote on Python

2012-11-12 Thread Rodrick Brown
I believe this statement is correct given key differences do exist in
underlying implementations even though such differences may be highly
transparent to end users (developers).


On Mon, Nov 12, 2012 at 10:08 PM, Mark Lawrence wrote:

> http://stackoverflow.com/**questions/tagged/python
>
> "Python has two major versions (2 and 3) in use which have significant
> differences."
>
> I believe that this is incorrect.  The warts have been removed, but
> significant differences, not in my book.  If there is agreement about there
> not being significant differences, should stackoverflow be asked to change
> their wording?
>
> --
> Cheers.
>
> Mark Lawrence.
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


stackoverflow quote on Python

2012-11-12 Thread Mark Lawrence

http://stackoverflow.com/questions/tagged/python

"Python has two major versions (2 and 3) in use which have significant 
differences."


I believe that this is incorrect.  The warts have been removed, but 
significant differences, not in my book.  If there is agreement about 
there not being significant differences, should stackoverflow be asked 
to change their wording?


--
Cheers.

Mark Lawrence.

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


Re: Simple Question regarding running .py program

2012-11-12 Thread Caroline Hou
On Monday, 12 November 2012 21:25:08 UTC-5, Dave Angel  wrote:
> On 11/12/2012 09:02 PM, Caroline Hou wrote:
> 
> > Hi all!
> 
> >
> 
> > I just started learning Python by myself and I have an extremely simple 
> > question now!
> 
> > I am in my Python interpreter now and I want to open/edit a program called 
> > nobel.py. But when I typed >>> python nobel.py, it gave me a 
> > "SyntaxError:invalid syntax”( I've changed to the correct directory)what 
> > should I do?
> 
> > I also want to run the program, but as I double-clicked the program, a 
> > command window pops up and closes immediately. How can I see the result of 
> > the program run?
> 
> > Could anyone help me please? I am pretty confused here...Thank you!
> 
> 
> 
> It'd be nice to specify that you're running Windows, and also what
> 
> version of the interpreter, although in this case the latter doesn't matter.
> 
> 
> 
> 
> 
> Go to a shell (cmd.exe), change to the directory containing that script,
> 
> and type the command as you did.
> 
> 
> 
> On linux:  davea@think:~$ python nobel.py
> 
> On Windows:   c:\mydir\myscript > python nobel.py
> 
> 
> 
> If you're already in the python interpreter, then running python is
> 
> useless -- it's already running.  In that case, you might want to use
> 
> import.  However, I recommend against it at first, as it opens up some
> 
> other problems you haven't experience with yet.
> 
> 
> 
> When you say you "double clicked the program', we have to guess you
> 
> might have meant in MS Explorer.  If you do that, it launches a cmd, it
> 
> runs the python system, and it closes the cmd.  Blame Windows for not
> 
> reading your mind.  If you want the cmd window to stick around, you
> 
> COULD end your program with an raw_input function call, but frequently
> 
> that won't work.  The right answer is the first one above...   Open a
> 
> shell (perhaps with a menu like  DOS BOX), change...
> 
> 
> 
> That way, when the program finishes, you can see what happened, or
> 
> didn't happen, and you can run it again using the uparrow key.
> 
> 
> 
> BTW, you don't need to send email to both the python-list and to the
> 
> newsgroup.  The newsgroup is automatically fed from the list.  But since
> 
> you're posting from google groups, that's just one of the bugs.  Many
> 
> folks here simply filter out everything from google groups, so your post
> 
> is invisible to them.
> 
>
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Hi Dave!

thank you very much for your quick reply! I did manage to get the program run 
from cmd.exe. 
So does it mean that if I want to use python interactively,I should use the 
interpreter,while if I just want to run a python program, I should use DOS 
shell instead?
Also, how could I edit my script? I have sth called "IDLE" installed along with 
python. Is it the right place to write/edit my script?
Sorry about these semi-idiot questions but it is really hard to find an article 
or book that covers such basic stuffs! 
Thank you!

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


Re: Simple Question regarding running .py program

2012-11-12 Thread Dave Angel
On 11/12/2012 09:02 PM, Caroline Hou wrote:
> Hi all!
>
> I just started learning Python by myself and I have an extremely simple 
> question now!
> I am in my Python interpreter now and I want to open/edit a program called 
> nobel.py. But when I typed >>> python nobel.py, it gave me a 
> "SyntaxError:invalid syntax”( I've changed to the correct directory)what 
> should I do?
> I also want to run the program, but as I double-clicked the program, a 
> command window pops up and closes immediately. How can I see the result of 
> the program run?
> Could anyone help me please? I am pretty confused here...Thank you!

It'd be nice to specify that you're running Windows, and also what
version of the interpreter, although in this case the latter doesn't matter.


Go to a shell (cmd.exe), change to the directory containing that script,
and type the command as you did.

On linux:  davea@think:~$ python nobel.py
On Windows:   c:\mydir\myscript > python nobel.py

If you're already in the python interpreter, then running python is
useless -- it's already running.  In that case, you might want to use
import.  However, I recommend against it at first, as it opens up some
other problems you haven't experience with yet.

When you say you "double clicked the program', we have to guess you
might have meant in MS Explorer.  If you do that, it launches a cmd, it
runs the python system, and it closes the cmd.  Blame Windows for not
reading your mind.  If you want the cmd window to stick around, you
COULD end your program with an raw_input function call, but frequently
that won't work.  The right answer is the first one above...   Open a
shell (perhaps with a menu like  DOS BOX), change...

That way, when the program finishes, you can see what happened, or
didn't happen, and you can run it again using the uparrow key.

BTW, you don't need to send email to both the python-list and to the
newsgroup.  The newsgroup is automatically fed from the list.  But since
you're posting from google groups, that's just one of the bugs.  Many
folks here simply filter out everything from google groups, so your post
is invisible to them.
   

-- 

DaveA

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


Simple Question regarding running .py program

2012-11-12 Thread Caroline Hou
Hi all!

I just started learning Python by myself and I have an extremely simple 
question now!
I am in my Python interpreter now and I want to open/edit a program called 
nobel.py. But when I typed >>> python nobel.py, it gave me a 
"SyntaxError:invalid syntax”( I've changed to the correct directory)what should 
I do?
I also want to run the program, but as I double-clicked the program, a command 
window pops up and closes immediately. How can I see the result of the program 
run?
Could anyone help me please? I am pretty confused here...Thank you!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Division matrix

2012-11-12 Thread Joshua Landau
On 13 November 2012 01:00, Cleuson Alves  wrote:

> Hello, I need to solve an exercise follows, first calculate the inverse
> matrix and then multiply the first matrix.
>

This list isn't to give answers for homeworks, and this sounds like one. We
*do* give help to those who have a specific problem and who preferably show
that they are trying to help up help them.


> I await help.
>

For what?! You haven't asked a question.
1) What do you need. Answer precisely, preferably giving an example output
for the input you want
2) What techniques have you tried? You have given code below in an
extremely un-obvious manner. What does it do, crash? Is it the wrong
output? What is wrong about the output?
3) What do you *think* has gone wrong? What about the output seems to be
wrong?


> Thank you.
> follows the code below incomplete.
>

On the basis that we can get some foreign people who maybe don't have
English as a commonly used language, muddled grammar isn't that big deal.
However, when asking for help it is worth checking that you've asked in a
clear way.


> m = [[1,2,3],[4,5,6],[7,8,9]]
> x = []
> for i in [0,1,2]:
> y = []
> for linha in m:
> y.append(linha[i])
> x.append(y)
>
> print x
> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>

Is this the right output? Is this what you *meant* by inverse? As Ian Kelly
(who is undoubtedly more learned in this area) said, this is a transpose,
so you have just swapped the one axis with another.


> def ProdMatrix(x,b):
> tamL = len(x)
> tamC = len(x[0])
> c = nullMatrix(tamL,tamC)
> for i in range(tamL):
> for j in range(tamC):
> val = 0
> for k in range(len(b)):
> val = val + x[i][l]*b[k][j]
> c[i][j]
> return c
>

You haven't given the full code. This crashes because we don't have
"nullMatrix" defined. It would be nice if we had something we could try to
run.

Then, the error is: "NameError: global name 'l' is not defined". On the
line "val = val + x[i][l]*b[k][j]" you use a variable "l" which doesn't
exist. What should you be using, or should you have created it?

*Then* you get an output of pure 0s. This is because you forgot to put your
result into c, your new matrix.
You probably just forgot to finish the line "c[i][j]", which does nothing
now.

You're actually really close on the second part. I have no idea if you're
doing what you want for the first part, but it's not what's traditionally
called an inverse.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive

2012-11-12 Thread Terry Reedy

On 11/12/2012 4:35 PM, Terry Reedy wrote:


import urllib.request
opener = urllib.request.build_opener()
request = urllib.request.Request("http://example.com/";, headers =
 {"Content-Type": "application/x-www-form-urlencoded"})

opener.open(request, "1".encode("us-ascii"))
print(request.data, '\n', request.header_items())

opener.open(request, "123456789".encode("us-ascii"))
print(request.data, '\n', request.header_items())

exhibits the same behavior in 3.3.0 of printing ('Content-length', '1')
in the last output. I agree that that looks wrong, but I do not know if
such re-use is supposed to be supported.


I opened http://bugs.python.org/issue16464

--
Terry Jan Reedy

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


Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive

2012-11-12 Thread Terry Reedy

On 11/12/2012 5:59 PM, Cameron Simpson wrote:

On 12Nov2012 16:35, Terry Reedy  wrote:
| On 11/12/2012 10:52 AM, Johannes Kleese wrote:
| > While at it, I noticed that urllib.request.Request.has_header() and
| > .get_header() are case-sensitive,
|
| Python is case sensitive.


To be more precise, Python string comparisons are by codepoints. If one 
wants normalized comparison, one usually has to do do the normalization 
oneself.



But headers are not. I'd be very inclined to consider case sensitivity
in has_header and get_header to be a bug, absent a compelling argument
against it.


It appears that the behavior is not consistent with doc examples and 
other header name handling. I added a note to

http://bugs.python.org/issue12455

--
Terry Jan Reedy

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


Re: Division matrix

2012-11-12 Thread Ian Kelly
On Mon, Nov 12, 2012 at 6:00 PM, Cleuson Alves  wrote:
> Hello, I need to solve an exercise follows, first calculate the inverse 
> matrix and then multiply the first matrix.
> I await help.
> Thank you.
> follows the code below incomplete.

So what is the specific problem with the code that you're looking for help with?

> m = [[1,2,3],[4,5,6],[7,8,9]]
> x = []
> for i in [0,1,2]:
> y = []
> for linha in m:
> y.append(linha[i])
> x.append(y)
>
> print x
> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

This calculates the transpose of the matrix, not the inverse.  If the
inverse is really what you're after, you should start by reading up on
the math to compute that.

Note that "for i in [0,1,2]:" could be more generally written as "for
i in range(len(m[0])):", and then you don't need to rewrite your for
loop if the dimensions of m change.

If you actually do want the transpose, then I'll also mention in
passing that there is a very nice one-liner using zip() to do this.
I'll leave the details as an exercise. ;-)

> def ProdMatrix(x,b):
> tamL = len(x)
> tamC = len(x[0])
> c = nullMatrix(tamL,tamC)
> for i in range(tamL):
> for j in range(tamC):
> val = 0
> for k in range(len(b)):
> val = val + x[i][l]*b[k][j]
> c[i][j]
> return c

In the multiplication line you're using the variable 'l' as an index,
but you haven't defined it.  Probably you wanted 'k' here.

You're also discarding 'val' after adding it up.  If you want that
value in the 'c' matrix, then you need to store it there before going
on to the next loop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Division matrix

2012-11-12 Thread Cleuson Alves
Hello, I need to solve an exercise follows, first calculate the inverse matrix 
and then multiply the first matrix.
I await help.
Thank you.
follows the code below incomplete.

m = [[1,2,3],[4,5,6],[7,8,9]]
x = []
for i in [0,1,2]:
y = []
for linha in m:
y.append(linha[i])
x.append(y)

print x
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

def ProdMatrix(x,b):
tamL = len(x)
tamC = len(x[0])
c = nullMatrix(tamL,tamC)
for i in range(tamL):
for j in range(tamC):
val = 0
for k in range(len(b)):
val = val + x[i][l]*b[k][j]
c[i][j]
return c
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help building a dictionary of lists

2012-11-12 Thread Joshua Landau
On 12 November 2012 22:26, NJ1706  wrote:

> # List of tests
> TestList = (
> 'Test_1',
> 'Test_2'
> )


Note that TestList is a *tuple*, not a list.

You normally would want to write "test_names" instead of "TestList" for
several reasons:

* Unless it's a class, Python prefers lowercase_names_with_underscores (or
sometimes camelCaseNamesThatLookLikeThis)
* Python tries not to care what type you have - that your names are in a
list is less important than that you can loop over it
* It's definitely not a list. A list would look like this:
TestList = [
'Test_1',
'Test_2'
]



Additionally, your comment here is somewhat pointless.  The code says
*exactly* what your comment does, so why write it twice?

# List of tests
Don't need

# Initialise the dictionary of lists
Don't need

# Loop through the list of tests
Don't need

# Append to the list for each instance
Not very helpful. If you comment here, you should explain *why* you are
looping, not *that* you are looping. You write "append", but *what* and,
more importantly,*why*?

# Initialise our string list
Don't need

# Build string list
Again, whilst it is nice to know more, you are not saying *what*
you are building. What is the *purpose* of the list?

# Convert to string
Don't need

# Assign to target list
Don't need


A good comment might just be one at the start that explains the goal, or
one inside that explains the *point* of a piece of code. Consider these two
commented pieces of code:


# Define a function that takes a, b and c
def f(a, b, c):

# Set s to ((the square of b) minus four times a times c) all to the
power of 0.5
s = (B**2 - 4 * a * c) ** 0.5

# Return the calculated values
return (- b + s) / (2 * a), (- b - s) / (2 * a)


Vs.


# Finds x where the equation "ax**2 + bx + c" makes 0
def f(a, b, c):
# Use the quadratic equation (
http://en.wikipedia.org/wiki/Quadratic_equation)

# s is the square root part of the equation (where x**0.5 is the square
root of x)
s = (B**2 - 4 * a * c) ** 0.5

# Return two values because there are two possible solutions
return (- b + s) / (2 * a), (- b - s) / (2 * a)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help building a dictionary of lists

2012-11-12 Thread Joshua Landau
On 12 November 2012 22:26, NJ1706  wrote:

> Chaps,
>
> I am new to Python & have inherited a test harness written in the language
> that I am trying to extend.
>
> The following code shows how dictionaries holding lists of commands are
> handled in the script...
>
> >>> Start of Code_1 <<<



> >>> End of Output_1 <<<
>
> Note that dict1 contains only the details of the particlare test, see YYY.
>
> This is a very cut down script compared to the real thing & in reality
> there are many more entries in the TestList and also there are many
> dictionaries. To make the script simpler to extend I would like to remove
> the need to manually create each of the dictionaries.
>
> After some reading around I found the dict.fromkeys() method & came up
> with the following...
>
> >>> Start of Code_2 <<<

  >>> End of Ouput_2 <<<
>
> This almost does what I want but dict2[Test_2] displayed at XXX contains
> the value for Test_1 as well as Test_2. I would be very grateful if someone
> can help me to get the line marked with XXX to be the same as YYY in code_1
> at the start.
>
> I am using Python 2.6.8 on Cygwin 1.7.17 but get the same results on
> CentOS 6.3
>

First of all, thank you for being thorough. It's always a help. *However*,
a minimalistic will be read faster usually, so you may want to crop down
what you have. Additionally, it can result in you solving it yourself!

The problem is as follows

"{1:[], 2:[], 3:[]}" does not equal "dict.fromkeys([1, 2, 3], [])"

You could have worked out this much very quickly, as it is the only thing
you change in the code.

Let's look:
>>> {1:[], 2:[], 3:[]}
{1: [], 2: [], 3: []}
>>> dict.fromkeys([1,2,3], [])
{1: [], 2: [], 3: []}
>>>

Oh no! They are the same! What has happened?
Well, they're *not*. In the first example each of the lists are *unique*:

>>> dct = {1:[], 2:[], 3:[]}
>>> dct[1] is dct[2]
False
>>> dct[1] is dct[3]
False
>>> dct[2] is dct[3]
False
>>>

In the second, they are all *the same list*:

>>> dct = dict.fromkeys([1,2,3], [])
>>> dct[1] is dct[2]
True
>>> dct[1] is dct[3]
True
>>> dct[2] is dct[3]
True
>>>

What does this mean?

>>> a = []
>>> b = []
>>> c = b
>>>
>>> a is b
False
>>> b is c
True
>>>
>>> a.append(1)
>>> b.append(2)
>>> c.append(3)
>>>
>>> a
[1]
>>> b
[2, 3]
>>> c
[2, 3]
>>>

Because b and c in this are *the same*, changing one *is* changing the
other. Hence, you've appended *two* things to b and c, but only one to a.

In your code you use .append on the items in your dict. This will have the
same problem as with a, b and c above.

If you want to make a dict, you can use a dictionary comprehension. I
assume you understand what a list comprehension is. If not, look here:
http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

Dict comprehension:
{i:[] for i in ["Test 1", "Test 2", "Test 3"]}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive

2012-11-12 Thread Cameron Simpson
On 12Nov2012 16:35, Terry Reedy  wrote:
| On 11/12/2012 10:52 AM, Johannes Kleese wrote:
| > While at it, I noticed that urllib.request.Request.has_header() and
| > .get_header() are case-sensitive,
| 
| Python is case sensitive.

But headers are not. I'd be very inclined to consider case sensitivity
in has_header and get_header to be a bug, absent a compelling argument
against it.
-- 
Cameron Simpson 

When a man rides a Motorader he stays forever young.- German saying
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: f python?

2012-11-12 Thread Steven D'Aprano
On Mon, 12 Nov 2012 17:37:50 -0500, Dave Angel wrote:

> On 11/12/2012 05:07 PM, Beekeeper2020 wrote:
[...]
>> Python eventually will die once troll troll troll troll troll...

> In case anybody is tempted to respond to this troll message, 

Like you did? Without trimming?

:-P




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


Re: f python?

2012-11-12 Thread Dave Angel
On 11/12/2012 05:07 PM, Beekeeper2020 wrote:
> I totally agreed about the Python syntax.  Why do I need to worry about the
> syntax which wasted hours to get it to work?
> Brain dead python designer!  Maybe Guido need to learn it from the Master,
> "Go to Ruby, and see how elegant the language is done.  Also, it is stupid
> of  google to hire Guido to prolong the death of Python.  No wonder a lot of
> engineers left google because they don't pay enough.  Vote for your feet,
> googlers.
>
> Python eventually will die once they found out Ruby don't have this dumb
> issues.  Guido, you're doing a wonderful job, wasting software programmers
> on hours solving your Python Stupid Syntax ( Your dumb Global variable is
> another example).
>
> Google is too dumb to understand these issues.
>
>

In case anybody is tempted to respond to this troll message, notice it's
a first-time poster, and it replies to a 7-month old message, and gives
no context.

Useless.


-- 

DaveA

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


Help building a dictionary of lists

2012-11-12 Thread NJ1706
Chaps,

I am new to Python & have inherited a test harness written in the language that 
I am trying to extend.

The following code shows how dictionaries holding lists of commands are handled 
in the script...

>>> Start of Code_1 <<<
#! /usr/bin/python

# List of tests
TestList = (
'Test_1',
'Test_2'
)

# Initialise the dictionary of lists
dict1 = {
'Test_1'  : [],
'Test_2'  : [],
}


instances = ('1')

# Loop through the list of tests
for Test in TestList:
print
print "Test: ", Test

# Append to the list for each instance
for instance in instances:
print "  instance: ", instance

# Initialise our string list
str_l = []

# Build string list
str_l.append ('ID %s' % Test)
str_l.append (' instance %s' % instance)

# Convert to string
str = ''.join (str_l)

print "str: ", str

# Assign to target list
dict1[Test].append('%s' % str)

print "dict1: ", dict1[Test]
>>> End of Code_1 <<<

This code produces the following output

>>> Start of Output_1 <<<
Test:  Test_1
  instance:  1
str:  ID Test_1 instance 1
dict1:  ['ID Test_1 instance 1']

Test:  Test_2
  instance:  1
str:  ID Test_2 instance 1
dict1:  ['ID Test_2 instance 1'] # YYY
>>> End of Output_1 <<<

Note that dict1 contains only the details of the particlare test, see YYY.

This is a very cut down script compared to the real thing & in reality there 
are many more entries in the TestList and also there are many dictionaries. To 
make the script simpler to extend I would like to remove the need to manually 
create each of the dictionaries.

After some reading around I found the dict.fromkeys() method & came up with the 
following...

>>> Start of Code_2 <<<

#! /usr/bin/python

TestList = (
'Test_1',
'Test_2'
)

dict2 = dict.fromkeys (TestList, [])

instances = ('1')


for Test in TestList:
print
print "Test: ", Test


for instance in instances:
print "  instance: ", instance

# Initialise our string list
str_l = []

# Build string list
str_l.append ('ID %s' % Test)
str_l.append (' instance %s' % instance)

# Convert to string
str = ''.join (str_l)

print "str: ", str

# Assign to target list
dict2[Test].append('%s' % str)

print "dict2: ", dict2[Test]

>>> End of Code_2 <<<

This produces the following output

>>> Start of Ouput_2 <<<
Test:  Test_1
  instance:  1
str:  ID Test_1 instance 1
dict2:  ['ID Test_1 instance 1']

Test:  Test_2
  instance:  1
str:  ID Test_2 instance 1
dict2:  ['ID Test_1 instance 1', 'ID Test_2 instance 1'] # XXX
>>> End of Ouput_2 <<<

This almost does what I want but dict2[Test_2] displayed at XXX contains the 
value for Test_1 as well as Test_2. I would be very grateful if someone can 
help me to get the line marked with XXX to be the same as YYY in code_1 at the 
start.

I am using Python 2.6.8 on Cygwin 1.7.17 but get the same results on CentOS 6.3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List comprehension for testing **params

2012-11-12 Thread Cantabile

Wow, lots of things I had never heard of in your posts.
I guess I need to do some homework...

Cantabile


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


Re: Writing Donald E. Knuth based code in Python, cont'd

2012-11-12 Thread Vincent Vande Vyvre
Le 12/11/12 22:02, Juhani Ylikoski a écrit :
> Following comes a working, debugged Python program which computes the
> permutations of the integers 1, 2, 3 - n after Donald E. Knuth.  I
> present it as an example of writing straightforward, easy Knuth-based
> code in a language with no GOTO statement.
>
> The Python program has been written after the DFA construct I
> previously discussed in this newsgroup, and after Knuth's discussion
> of the solution of the problem; and according the (very good)
> discussions in this newsgroup.  To my opinion, it no more is a "crow's
> nest" as they say in Finnish.
>
> This program was needed for a real problem, namely computing optimal
> tournament tables for a Bughouse (Tandem) chess tournament.  See
>
> http://en.wikipedia.org/wiki/Bughouse_chess
>
> Knuth became criticized in the newsgroup; but to my opinion his books
> are still useful and nontrivially needed.
>
>
> ---
>
>
> yours sincerely, Antti J Ylikoski
> Helsinki, Finland
> PhD student in the Aalto University
>
Thanks,

One comment in:

   def E1(self): # Phase 1 in Knuth's text
   self.app = self.listofPerm.append(self.a[1:self.n+1])
   return self.E2 # next state: E2

append() return None and self.app is no longer used in the code.

Missing something ?

-- 
Vincent V.V.
Oqapy  . Qarte
 . PaQager 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: f python?

2012-11-12 Thread Beekeeper2020
I totally agreed about the Python syntax.  Why do I need to worry about the
syntax which wasted hours to get it to work?
Brain dead python designer!  Maybe Guido need to learn it from the Master,
"Go to Ruby, and see how elegant the language is done.  Also, it is stupid
of  google to hire Guido to prolong the death of Python.  No wonder a lot of
engineers left google because they don't pay enough.  Vote for your feet,
googlers.

Python eventually will die once they found out Ruby don't have this dumb
issues.  Guido, you're doing a wonderful job, wasting software programmers
on hours solving your Python Stupid Syntax ( Your dumb Global variable is
another example).

Google is too dumb to understand these issues.




--
View this message in context: 
http://python.6.n6.nabble.com/f-python-tp4708177p4995649.html
Sent from the Python - python-list mailing list archive at Nabble.com.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread jkn
Hi Hans

[...]
>
> >         xargsproc.append("test -f %s/{} && md5sum %s/{}" % (mydir,
> > mydir))
>
> This will break if there are spaces in the file name, or other
> characters meaningful to the shell.  If you change if to
>
>         xargsproc.append("test -f '%s/{}' && md5sum '%s/{}'"
>                              % (mydir, mydir))
>
> , then it will only break if there are single quotes in the file name.

Fair point. As it happens, I know that there are no 'unhelpful'
characters in the filenames ... but it's still worth doing.

>
> As I understand, your plan is to rewrite this bit in pure Python, to
> get rid of any and all such problems.

Yep - as mentioned in another reply I wanted first to have something
which duplicated the current action (which has taken longer than I
expected), and then rework in a more pythonic way.

Still, I've learned some things about the subprocess module, and also
about the shell, so it's been far from wasted time.

Regards
Jon N
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive

2012-11-12 Thread Terry Reedy

On 11/12/2012 10:52 AM, Johannes Kleese wrote:

Hi!

(Yes, I did take a look at the issue tracker but couldn't find any
corresponding bug, and no, I don't want to open a new account just for
this one.)


You only have to open a tracker account just once. I am reluctant to 
report this myself as I do not use the module and cannot answer questions.



I'm reusing a single urllib.request.Request object to HTTP-POST data to
the same URL a number of times. While the data itself is sent as
expected every time, the Content-Length header is not updated after the
first request. Tested with Python 3.1.3 and Python 3.1.4.


3.1 only gets security fixes. Consider upgrading. In any case, suspected 
bugs need to be tested with the latest release, as patches get applied 
daily. As it happens,


import urllib.request
opener = urllib.request.build_opener()
request = urllib.request.Request("http://example.com/";, headers =
{"Content-Type": "application/x-www-form-urlencoded"})

opener.open(request, "1".encode("us-ascii"))
print(request.data, '\n', request.header_items())

opener.open(request, "123456789".encode("us-ascii"))
print(request.data, '\n', request.header_items())

exhibits the same behavior in 3.3.0 of printing ('Content-length', '1') 
in the last output. I agree that that looks wrong, but I do not know if 
such re-use is supposed to be supported.




While at it, I noticed that urllib.request.Request.has_header() and
.get_header() are case-sensitive,


Python is case sensitive.


while HTTP headers are not (RFC 2616, 4.2).

> Thus the following, slightly unfortunate behaviour:



request.header_items()

[('Content-length', '1'), ('Content-type',
'application/x-www-form-urlencoded'), ('Host', 'example.com'),
('User-agent', 'Python-urllib/3.1')]


request.has_header("Content-Type")

False

request.has_header("Content-type")

True

request.get_header("Content-Type")

# this return None, which is not printed

request.get_header("Content-type")

'application/x-www-form-urlencoded'


Judging from 'Content-type', 'User-agent', 'Content-length', 'Host', 
urllib.request consistently capitalizes the first word of all header 
tags and expects them in that form. If that is not standard, it should 
be documented.


--
Terry Jan Reedy

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


Writing Donald E. Knuth based code in Python, cont'd

2012-11-12 Thread Juhani Ylikoski

Following comes a working, debugged Python program which computes the
permutations of the integers 1, 2, 3 - n after Donald E. Knuth.  I
present it as an example of writing straightforward, easy Knuth-based
code in a language with no GOTO statement.

The Python program has been written after the DFA construct I
previously discussed in this newsgroup, and after Knuth's discussion
of the solution of the problem; and according the (very good)
discussions in this newsgroup.  To my opinion, it no more is a "crow's
nest" as they say in Finnish.

This program was needed for a real problem, namely computing optimal
tournament tables for a Bughouse (Tandem) chess tournament.  See

http://en.wikipedia.org/wiki/Bughouse_chess

Knuth became criticized in the newsgroup; but to my opinion his books
are still useful and nontrivially needed.

--

class DFA(object):

   # Iteratively generate all permutations of n integers 1-n.
   # After Donald Knuth, The Art of Computer Programming, Vol4, Fascicle 2,
   # ISBN 0-201-85393-0, on Pages 39-40.

   def __init__(self, n):
   self.n = n
   self.listofPerm = [] # list of lists to collect permutations
   self.nextStat = self.E1 # next phase in Knuth's text
   self.a = list(range(0, n+1)) # [0, 1, 2, 3, 4, ..., n] -- see Knuth

   def E1(self): # Phase 1 in Knuth's text
   self.app = self.listofPerm.append(self.a[1:self.n+1])
   return self.E2 # next state: E2

   def E2(self): # Phase 2 in Knuth's text
   self.j = self.n - 1
   while self.a[self.j] >= self.a[self.j+1]:
   self.j -= 1
   if self.j == 0:
   return None # algorithm finishes
   else:
   return self.E3 # next state: E3

   def E3(self): # Phase 3 in Knuth
   self.l = self.n
   while self.a[self.j] >= self.a[self.l]:
   self.l -= 1
   self.temp = self.a[self.j]
   self.a[self.j] = self.a[self.l]
   self.a[self.l] = self.temp
   return self.E4 # next state: E4

   def E4(self): # Phase 4
   self.k = self.j + 1
   self.l = self.n
   while self.k < self.l:
   self.temp = self.a[self.k]
   self.a[self.k] = self.a[self.l]
   self.a[self.l] = self.temp
   self.k += 1
   self.l -= 1
   return self.E1 # following phase: Phase 1

   def runDFA(self):
   self.nextState = self.E1
   while self.nextState is not None:
   self.nextState = self.nextState()
   return(self.listofPerm)


--


yours sincerely, Antti J Ylikoski
Helsinki, Finland
PhD student in the Aalto University

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


Re: Is there a way to creat a func that returns a cursor that can be used?

2012-11-12 Thread Terry Reedy

On 11/12/2012 7:25 AM, Joel Goldstick wrote:


Chris gave you the same help that you got yesterday.

...

go to ://python.org> and read the tutorials,
specifically about functions.


It is hard to see what is working and not with an empty database. But to 
drive the point home, running


import sqlite3

def connect():
conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db   etc.
cur = conn.cursor()
cur.execute("create table schedule (teams integer, sn integer, 
badge integer ,name text, grp integer,\
major text, track text, stage text,  tc text, subject text, course 
text, ws text, date text, \
time text, proctor text, code text, no integer,fl_time text, flag2 
text, flag3 text,\

flag4 text, clash1 integer, clash2 integer)")
return cur

cur = connect()
print(cur.fetchone())
cur.execute("select * from schedule")
print(cur.fetchall())

# prints
None
[]

So the change suggested by multiple people *does* work ;-).

On a different note: use triple quote for multi-line strings and 
backslashes are not needed.


cur.execute('''create table schedule (teams integer, sn integer,
badge integer ,name text, grp integer, major text,
track text, stage text,  tc text, subject text, course text,
ws text, date text, time text, proctor text, code text,
no integer,fl_time text, flag2 text, flag3 text, flag4 text,
clash1 integer, clash2 integer)''')

works fine. SQL treats newlines in statements as whitespace.

--
Terry Jan Reedy

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread Hans Mulder
On 12/11/12 18:22:44, jkn wrote:
> Hi Hans
> 
> On Nov 12, 4:36 pm, Hans Mulder  wrote:
>> On 12/11/12 16:36:58, jkn wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> slight followup ...
>>
>>> I have made some progress; for now I'm using subprocess.communicate to
>>> read the output from the first subprocess, then writing it into the
>>> secodn subprocess. This way I at least get to see what is
>>> happening ...
>>
>>> The reason 'we' weren't seeing any output from the second call (the
>>> 'xargs') is that as mentioned I had simplified this. The actual shell
>>> command was more like (in python-speak):
>>
>>> "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir)
>>
>>> ie. I am running md5sum on each tar-file entry which passes the 'is
>>> this a file' test.
>>
>>> My next problem; how to translate the command-string clause
>>
>>> "test -f %s/{} && md5sum %s/{}" # ...
>>
>>> into s parameter to subprocss.Popen(). I think it's the command
>>> chaining '&&' which is tripping me up...
>>
>> It is not really necessary to translate the '&&': you can
>> just write:
>>
>> "test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir)
>>
>> , and xargs will pass that to the shell, and then the shell
>> will interpret the '&&' for you: you have shell=False in your
>> subprocess.Popen call, but the arguments to xargs are -I {}
>> sh -c "", and this means that xargs ends up invoking the
>> shell (after replacing the {} with the name of a file).
>>
>> Alternatively, you could translate it as:
>>
>> "if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi" % (mydir, mydir)
>>
>> ; that might make the intent clearer to whoever gets to
>> maintain your code.
> 
> Yes to both points; turns out that my problem was in building up the
> command sequence to subprocess.Popen() - when to use, and not use,
> quotes etc. It has ended up as (spelled out in longhand...)
> 
> 
> xargsproc = ['xargs']
> 
> xargsproc.append('-I')
> xargsproc.append("{}")
> 
> xargsproc.append('sh')
> xargsproc.append('-c')
> 
> xargsproc.append("test -f %s/{} && md5sum %s/{}" % (mydir,
> mydir))

This will break if there are spaces in the file name, or other
characters meaningful to the shell.  If you change if to

xargsproc.append("test -f '%s/{}' && md5sum '%s/{}'"
 % (mydir, mydir))

, then it will only break if there are single quotes in the file name.

As I understand, your plan is to rewrite this bit in pure Python, to
get rid of any and all such problems.

> As usual, breaking it all down for the purposes of clarification has
> helpd a lot, as has your input. Thanks a lot.

You're welcome.

-- HansM


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


Re: Read number of CSV files

2012-11-12 Thread Smaran Harihar
Oh that is great.

[apology on sending the mail directly]

Thanks,
Smaran

On Mon, Nov 12, 2012 at 10:50 AM, Peter Otten <__pete...@web.de> wrote:

> Peter Otten wrote:
>
> [please don't email me directly]
>
> > How is using glob different from os.listdir() Peter?
>
> glob retains the path and allows you to filter the files. Compare:
>
> >>> import os, glob
> >>> os.listdir("alpha")
> ['one.py', 'two.py', 'one.txt', 'three.py', 'three.txt', 'two.txt']
> >>> glob.glob("alpha/*")
> ['alpha/one.py', 'alpha/two.py', 'alpha/one.txt', 'alpha/three.py',
> 'alpha/three.txt', 'alpha/two.txt']
> >>> glob.glob("alpha/*.py")
> ['alpha/one.py', 'alpha/two.py', 'alpha/three.py']
>
> See the documentation for more.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Thanks & Regards
Smaran Harihar
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read number of CSV files

2012-11-12 Thread Peter Otten
Peter Otten wrote:

[please don't email me directly]

> How is using glob different from os.listdir() Peter?

glob retains the path and allows you to filter the files. Compare:

>>> import os, glob
>>> os.listdir("alpha")
['one.py', 'two.py', 'one.txt', 'three.py', 'three.txt', 'two.txt']
>>> glob.glob("alpha/*")
['alpha/one.py', 'alpha/two.py', 'alpha/one.txt', 'alpha/three.py', 
'alpha/three.txt', 'alpha/two.txt']
>>> glob.glob("alpha/*.py")
['alpha/one.py', 'alpha/two.py', 'alpha/three.py']

See the documentation for more.

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread jkn
Hi Hans

On Nov 12, 4:36 pm, Hans Mulder  wrote:
> On 12/11/12 16:36:58, jkn wrote:
>
>
>
>
>
>
>
>
>
> > slight followup ...
>
> > I have made some progress; for now I'm using subprocess.communicate to
> > read the output from the first subprocess, then writing it into the
> > secodn subprocess. This way I at least get to see what is
> > happening ...
>
> > The reason 'we' weren't seeing any output from the second call (the
> > 'xargs') is that as mentioned I had simplified this. The actual shell
> > command was more like (in python-speak):
>
> > "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir)
>
> > ie. I am running md5sum on each tar-file entry which passes the 'is
> > this a file' test.
>
> > My next problem; how to translate the command-string clause
>
> >     "test -f %s/{} && md5sum %s/{}" # ...
>
> > into s parameter to subprocss.Popen(). I think it's the command
> > chaining '&&' which is tripping me up...
>
> It is not really necessary to translate the '&&': you can
> just write:
>
>     "test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir)
>
> , and xargs will pass that to the shell, and then the shell
> will interpret the '&&' for you: you have shell=False in your
> subprocess.Popen call, but the arguments to xargs are -I {}
> sh -c "", and this means that xargs ends up invoking the
> shell (after replacing the {} with the name of a file).
>
> Alternatively, you could translate it as:
>
>     "if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi" % (mydir, mydir)
>
> ; that might make the intent clearer to whoever gets to
> maintain your code.

Yes to both points; turns out that my problem was in building up the
command sequence to subprocess.Popen() - when to use, and not use,
quotes etc. It has ended up as (spelled out in longhand...)


xargsproc = ['xargs']

xargsproc.append('-I')
xargsproc.append("{}")

xargsproc.append('sh')
xargsproc.append('-c')

xargsproc.append("test -f %s/{} && md5sum %s/{}" % (mydir,
mydir))


As usual, breaking it all down for the purposes of clarification has
helpd a lot, as has your input. Thanks a lot.

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread jkn
On Nov 12, 4:58 pm, Rebelo  wrote:
> Dana četvrtak, 8. studenoga 2012. 19:05:12 UTC+1, korisnik jkn napisao je:
>
> > Hi All
>
> >     i am trying to build up a set of subprocess.Ponen calls to
>
> > replicate the effect of a horribly long shell command. I'm not clear
>
> > how I can do one part of this and wonder if anyone can advise. I'm on
>
> > Linux, fairly obviously.
>
> >     J^n
>
> You should try to do it in pure python, avoiding shell altogether.
> The first step would be to actually write what it is you want to do.
>

Hi Rebelo
   FWIW I intend to do exactly this - but I wanted to duplicate the
existing shell action beforehand, so that I could get rid of the shell
command.

After I've tidied things up, that will be my next step.

Cheers
Jon N



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


Re: Strange object identity problem

2012-11-12 Thread duncan smith

On 12/11/12 13:40, F.R. wrote:

On 11/12/2012 02:27 PM, Robert Franke wrote:

Hi Frederic,

[...]


bas = {}
for year in range (2010, 2013):

 ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year)
 ba.run ()
print year, id (ba)
 bas [year] = ba

2010 150289932
2011 150835852
2012 149727788


for y in sorted (bas.keys ()):

 b = bas [year]

Shouldn't that be b = bas[y]?

Yes, it should, indeed! What's more, I should have closed and restarted
IDLE. There must have
been a name clash somewhere in the name space. The problem no longer
exists. Sorry
about that. And thanks to all who paused to reflect on this non-problem.
- Frederic.





 print y, id (b)

2010 149727788
2011 149727788
2012 149727788


[...]

Cheers,

Robert





The problem was that year was bound to the integer 2013 from the first 
loop. When you subsequently looped over the keys you printed each key 
followed by id(bas[2013]). Restarting IDLE only helped because you 
presumably didn't repeat the error.


Duncan

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread Rebelo
Dana četvrtak, 8. studenoga 2012. 19:05:12 UTC+1, korisnik jkn napisao je:
> Hi All
> 
> i am trying to build up a set of subprocess.Ponen calls to
> 
> replicate the effect of a horribly long shell command. I'm not clear
> 
> how I can do one part of this and wonder if anyone can advise. I'm on
> 
> Linux, fairly obviously.
> 
> J^n

You should try to do it in pure python, avoiding shell altogether. 
The first step would be to actually write what it is you want to do.

To filter files you want to add to tar file check tarfile 
(http://docs.python.org/2/library/tarfile.html?highlight=tar#module-tarfile), 
specifically :
TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
which takes filter paramter :
"If filter is specified it must be a function that takes a TarInfo object 
argument and returns the changed TarInfo object. If it instead returns None the 
TarInfo object will be excluded from the archive."

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


Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive

2012-11-12 Thread Johannes Kleese
Hi!

(Yes, I did take a look at the issue tracker but couldn't find any
corresponding bug, and no, I don't want to open a new account just for
this one.)



I'm reusing a single urllib.request.Request object to HTTP-POST data to
the same URL a number of times. While the data itself is sent as
expected every time, the Content-Length header is not updated after the
first request. Tested with Python 3.1.3 and Python 3.1.4.

>>> opener = urllib.request.build_opener()
>>> request = urllib.request.Request("http://example.com/";, headers =
{"Content-Type": "application/x-www-form-urlencoded"})


>>> opener.open(request, "1".encode("us-ascii"))

>>> request.data
b'1'
>>> request.header_items()
[('Content-length', '1'), ('Content-type',
'application/x-www-form-urlencoded'), ('Host', 'example.com'),
('User-agent', 'Python-urllib/3.1')]


>>> opener.open(request, "123456789".encode("us-ascii"))

>>> request.data
b'123456789'
>>> request.header_items()
[('Content-length', '1'), ('Content-type',
'application/x-www-form-urlencoded'), ('Host', 'example.com'),
('User-agent', 'Python-urllib/3.1')]

Note that after the second run, Content-Length stays "1", but should be
"9", corresponding to the data b'123456789'. (Request data is not
x-www-form-urlencoded to shorten the test case. Doesn't affect the bug,
though.)






While at it, I noticed that urllib.request.Request.has_header() and
.get_header() are case-sensitive, while HTTP headers are not (RFC 2616,
4.2). Thus the following, slightly unfortunate behaviour:

>>> request.header_items()
[('Content-length', '1'), ('Content-type',
'application/x-www-form-urlencoded'), ('Host', 'example.com'),
('User-agent', 'Python-urllib/3.1')]

>>> request.has_header("Content-Type")
False
>>> request.has_header("Content-type")
True
>>> request.get_header("Content-Type")
>>> request.get_header("Content-type")
'application/x-www-form-urlencoded'



Thanks for taking care.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread Hans Mulder
On 12/11/12 16:36:58, jkn wrote:
> slight followup ...
> 
> I have made some progress; for now I'm using subprocess.communicate to
> read the output from the first subprocess, then writing it into the
> secodn subprocess. This way I at least get to see what is
> happening ...
> 
> The reason 'we' weren't seeing any output from the second call (the
> 'xargs') is that as mentioned I had simplified this. The actual shell
> command was more like (in python-speak):
> 
> "xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir)
> 
> ie. I am running md5sum on each tar-file entry which passes the 'is
> this a file' test.
> 
> My next problem; how to translate the command-string clause
> 
> "test -f %s/{} && md5sum %s/{}" # ...
> 
> into s parameter to subprocss.Popen(). I think it's the command
> chaining '&&' which is tripping me up...

It is not really necessary to translate the '&&': you can
just write:

"test -f '%s/{}' && md5sum '%s/{}'" % (mydir, mydir)

, and xargs will pass that to the shell, and then the shell
will interpret the '&&' for you: you have shell=False in your
subprocess.Popen call, but the arguments to xargs are -I {}
sh -c "", and this means that xargs ends up invoking the
shell (after replacing the {} with the name of a file).

Alternatively, you could translate it as:

"if [ -f '%s/{}' ]; then md5sum '%s/{}'; fi" % (mydir, mydir)

; that might make the intent clearer to whoever gets to
maintain your code.


Hope this helps,

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


Re: A gnarly little python loop

2012-11-12 Thread Steve Howell
On Nov 12, 7:21 am, rusi  wrote:
> On Nov 12, 12:09 pm, rusi  wrote:> This is a classic 
> problem -- structure clash of parallel loops
>
> 
>
> Sorry wrong solution :D
>
> The fidgetiness is entirely due to python not allowing C-style loops
> like these:
>
> >> while ((c=getchar()!= EOF) { ... }
> [...]

There are actually three fidgety things going on:

 1. The API is 1-based instead of 0-based.
 2. You don't know the number of pages in advance.
 3. You want to process tweets, not pages of tweets.

Here's yet another take on the problem:

# wrap fidgety 1-based api
def search(i):
return api.GetSearch("foo", i+1)

paged_tweets = (search(i) for i in count())

# handle sentinel
paged_tweets = iter(paged_tweets.next, [])

# flatten pages
tweets = chain.from_iterable(paged_tweets)
for tweet in tweets:
process(tweet)

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


Re: A gnarly little python loop

2012-11-12 Thread Peter Otten
rusi wrote:

> The fidgetiness is entirely due to python not allowing C-style loops
> like these:
> >>> while ((c=getchar()!= EOF) { ... }

for c in iter(getchar, EOF):
...

> Clearly the fidgetiness is there as before and now with extra coroutine
> plumbing

Hmm, very funny...


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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread jkn
slight followup ...

I have made some progress; for now I'm using subprocess.communicate to
read the output from the first subprocess, then writing it into the
secodn subprocess. This way I at least get to see what is
happening ...

The reason 'we' weren't seeing any output from the second call (the
'xargs') is that as mentioned I had simplified this. The actual shell
command was more like (in python-speak):

"xargs -I {} sh -c \"test -f %s/{} && md5sum %s/{}\"" % (mydir, mydir)

ie. I am running md5sum on each tar-file entry which passes the 'is
this a file' test.

My next problem; how to translate the command-string clause

"test -f %s/{} && md5sum %s/{}" # ...

into s parameter to subprocss.Popen(). I think it's the command
chaining '&&' which is tripping me up...

Cheers
J^n



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


Re: A gnarly little python loop

2012-11-12 Thread rusi
On Nov 12, 12:09 pm, rusi  wrote:
> This is a classic problem -- structure clash of parallel loops


Sorry wrong solution :D

The fidgetiness is entirely due to python not allowing C-style loops
like these:
>> while ((c=getchar()!= EOF) { ... }


Putting it into coroutine form, it becomes something like the
following [Untested since I dont have the API]. Clearly the
fidgetiness is there as before and now with extra coroutine plumbing

def genStage(term, target):
  page = 1
  while 1:
r = api.GetSearch(term="foo", page=page)
if not r:break
for tweet in r:  target.send(tweet)
page += 1


@consumer
def endStage():
while True: process((yield))

if __name__ == '__main__':
genStage("foo", endStage())
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error

2012-11-12 Thread Dave Angel
On 11/09/2012 09:08 AM, inshu chauhan wrote:
> Actually this one.. and its the last..
> 
> 
>

 The only extra libary i am using is Opencv , downloaded from
 http://sourceforge.net/projects/opencvlibrary/



and numpy.


> 3) what import statement did you use ?
>

 import cv



> 5) Exactly what error message did you get, including the traceback ?
>

  Traceback (most recent call last):
   File "Z:\modules\Masking_an_image_dynamically.py", line 155, in
 
 AccessPixels(data)
   File "Z:\modules\.py", line 147, in AccessPixels
 CreateMask(data, x, y)
   File "Z:\modules\new_classification.py", line 110, in CreateMask
 point = data[iy, ix ]

 error: index is out of range

 The line numbers here and the file attached may be different because I
 have removed a lot of print statements which I was using to debug the
 error..


More than that is wrong.  The file names are different.  If I guess from
your source file that all this code is in your own script,
"Masking_an_image_dynamically.py"   then two of the filenames in the
traceback indicate big problems.

Further, on the same assumption, the function CreateMask has no such
line as'point = data[iy, ix]

I did spend some time with the website
http://sourceforge.net/projects/opencvlibrary/

but all I see is C++ docs, and since it's enormous, I didn't think I'm
likely to find out anything directly from there.  The particular thing I
was looking for was whether rows or columns are specified first when
treating 'data" as a list of lists.In other words, I look at the two
lines:

CreateMask(data, x, y)
and
point = data[iy, ix]

and try to figure out which one is unreasonable.  Find out the
convention used in the module, and stay consistent with it.  In
Photoshop. an 8x10 is a portrait layout, while 10x8 is landscape.  In
other words, row number is first.  But i have no idea what Opencv uses.

Since I've not used either Opencv nor numpy, somebody else had better
jump in after you post the traceback that matches your source file.
Perhaps it's IDLE that's getting confused.  Have you tried running it
from a cmd.exe prompt, and pasting from that cmd window into a message?



-- 

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


Re: int.__init__ incompatible in Python 3.3

2012-11-12 Thread Ulrich Eckhardt

Am 09.11.2012 12:37, schrieb Steven D'Aprano:

In Python 3.3:

py> class X(int):
... def __init__(self, *args):
... super().__init__(*args)  # does nothing, call it anyway
...
py> x = X(22)
Traceback (most recent call last):
   File "", line 1, in 
   File "", line 3, in __init__
TypeError: object.__init__() takes no parameters


It is apparently an oversight, or a bug, that it ever worked in older
versions.



I'm not really convinced that the overall behaviour is sound:

py> x = 42
py> x.__init__()
py> x.__init__(1)
py> x.__init__(1,2)
py> x.__init__(1,2,3)
py> x.__init__(1,2,3,4)

Neither of these seem to care about the number and type of parameters. 
On the other hand:


py> y = object()
py> y.__init__()
py> y.__init__(1)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: object.__init__() takes no parameters


So, for some reason that I don't understand yet, my call to the 
superclass' init function skips a class, but only when called with super().



Confused greetings!

Uli

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


Re: Strange object identity problem

2012-11-12 Thread Ulrich Eckhardt

Am 12.11.2012 14:12, schrieb F.R.:

Once in a while I write simple routine stuff and spend the next few hours
trying to understand why it doesn't behave as I expect. Here is an example
holding me up:

[...snip incomplete code...]

Trying something similar with a simpler class works as expected:

[...snip example code...]

Okay, that's almost a classic. You ask about code that fails, while 
providing code that works as example. Crystal balls are rare nowadays, 
so this is really hard to answer!


In any case, here's what you could do:
1. use a debugger (import pdb...)
2. some more info could be retrieved by outputting the actual type along 
with the ID of the objects in question (see type() function)
3. reduce the non-working code until you have a minimal example that you 
can post here


I'd bet that at latest while trying approach 3 above, you will find the 
error yourself.


Good luck!

Uli

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


Re: Strange object identity problem

2012-11-12 Thread F.R.

On 11/12/2012 02:27 PM, Robert Franke wrote:

Hi Frederic,

[...]


bas = {}
for year in range (2010, 2013):

 ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year)
 ba.run ()
print year, id (ba)
 bas [year] = ba

2010 150289932
2011 150835852
2012 149727788


for y in sorted (bas.keys ()):

 b = bas [year]

Shouldn't that be b = bas[y]?
Yes, it should, indeed! What's more, I should have closed and restarted 
IDLE. There must have
been a name clash somewhere in the name space. The problem no longer 
exists. Sorry
about that. And thanks to all who paused to reflect on this non-problem. 
- Frederic.






 print y, id (b)

2010 149727788
2011 149727788
2012 149727788


[...]

Cheers,

Robert



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


Re: List comprehension for testing **params

2012-11-12 Thread Joshua Landau
On 12 November 2012 13:23, Joshua Landau  wrote:

> Just a few tricks you may have missed:
>
>  On 12 November 2012 10:48, Ulrich Eckhardt <
> ulrich.eckha...@dominolaser.com> wrote:
>
>> Am 11.11.2012 23:24, schrieb Cantabile:
>
> if required.intersection(params.**keys()) != required:
>>
>
> if required.issubset(params):
>

*Ahem*: if *not* required.issubset(params):


>
>
>>missing = required - set(params.keys())
>>
>
> missing = required.difference(params)
>
>
>>raise Exception("missing arguments {}".format(
>>', '.join(missing)))
>
>
>
> (untested)
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange object identity problem

2012-11-12 Thread Robert Franke
Hi Frederic,

[...]

 bas = {}
 for year in range (2010, 2013):
> ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year)
> ba.run ()
> print year, id (ba)
> bas [year] = ba
>
> 2010 150289932
> 2011 150835852
> 2012 149727788
>
 for y in sorted (bas.keys ()):
> b = bas [year]

Shouldn't that be b = bas[y]?


> print y, id (b)
>
> 2010 149727788
> 2011 149727788
> 2012 149727788
>
[...]

Cheers,

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


Re: List comprehension for testing **params

2012-11-12 Thread Joshua Landau
Just a few tricks you may have missed:

On 12 November 2012 10:48, Ulrich Eckhardt
wrote:

> Am 11.11.2012 23:24, schrieb Cantabile:

if required.intersection(params.**keys()) != required:
>

if required.issubset(params):


>missing = required - set(params.keys())
>

missing = required.difference(params)


>raise Exception("missing arguments {}".format(
>', '.join(missing)))



(untested)
-- 
http://mail.python.org/mailman/listinfo/python-list


Strange object identity problem

2012-11-12 Thread F.R.

Hi all,

Once in a while I write simple routine stuff and spend the next few hours
trying to understand why it doesn't behave as I expect. Here is an example
holding me up: I have a module "st" with a class "runs". In a loop I 
repeatedly

create an object "ba" and call the method "ba.run ()" which processes the
constructor's arguments. Next I store the object in a dictionary "bas". It
then turns out that all keys hold the same object, namely the one created
last in the loop.
 Verifying the identity of each object when it is being assigned to
the dictionary reveals different identities. Repeating the verification
after the loop is done shows the same object in all key positions:

>>> bas = {}
>>> for year in range (2010, 2013):
ba = st.runs ('BA', '%d-01-01' % year, '%d-12-31' % year)
ba.run ()
print year, id (ba)
bas [year] = ba

2010 150289932
2011 150835852
2012 149727788

>>> for y in sorted (bas.keys ()):
b = bas [year]
print y, id (b)

2010 149727788
2011 149727788
2012 149727788



The class "runs" has a bunch of attributes, among which an object 
"parameters"
for tweaking processing runs and a object "quotes" containing a list of 
data

base records. Both objects are created by "runs.__init__ (...)".

Trying something similar with a simpler class works as expected:

>>> class C:
def __init__ (self, i):
self.i = i
def run (self):
self.ii = self.i * self.i

>>> cees = {}
>>> for year in range (2010, 2013):
c = C (year)
c.run ()
print year, id (c)
cees [year] = c

2010 150837804
2011 148275756
2012 146131212

>>> for year in sorted (cees.keys ()):
print year, id (cees [year]), cees [year].ii

2010 150837804 4040100
2011 148275756 4044121
2012 146131212 4048144




I have checked for name clashes and found none, wondering what to check
next for. Desperate for suggestions.


Frederic


(Python 2.7 on Ubuntu 12.04)

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-12 Thread jkn
Hi Hans
thanks a lot for your reply:

> That's what 'xargs' will do for you.  All you need to do, is invoke
> xargs with arguments containing '{}'.  I.e., something like:
>
> cmd1 = ['tar', '-czvf', 'myfile.tgz', '-c', mydir, 'mysubdir']
> first_process = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
>
> cmd2 = ['xargs', '-I', '{}', 'sh', '-c', "test -f %s/'{}'" % mydir]
> second_process = subprocess.Popen(cmd2, stdin=first_process.stdout)
>

Hmm - that's pretty much what I've been trying. I will have to
experiment a bit more and post the results in a bit more detail.

> > Apologies if I've made any howlers in this description - it's very
> > likely...
>

> I think the second '-c' argument to tar should have been a '-C'.

You are correct, thanks. Serves me right for typing the simplified
version in by hand. I actually use the equivalent "--directory=..." in
the actual code.

> I'm not sure I understand what the second command is trying to
> achieve.  On my system, nothing happens, because tar writes the
> names of the files it is adding to stderr, so xargs receives no
> input at all.  If I send the stderr from tar to the stdin of
> xargs, then it still doesn't seem to do anything sensible.

That's interesting ... on my system, and all others that I know about,
the file list goes to stdout.

> Perhaps your real xargs command is more complicated and more
> sensible.

Yes, in fact the output from xargs is piped to a third process. But I
realise this doesn't alter the result of your experiment; the xargs
process should filter a subset of the files being fed to it.

I will experiment a bit more and hopefully post some results. Thanks
in the meantime...

Regards
Jon N

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


Re: Is there a way to creat a func that returns a cursor that can be used?

2012-11-12 Thread Joel Goldstick
On Mon, Nov 12, 2012 at 6:01 AM, Chris Angelico  wrote:

> On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi 
> wrote:
> > Is there a way to create a func that returns a cursor that can be used
> to execute sql statements?
>
> Yes, and you're almost there!
>
> > I tried this (after importing sqlite3), but it gave me the error below:
> >
>  def connect():
> > return cur
>  connect()
> > 
>  cur.execute("select * from schedule")
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > cur.execute("select * from schedule")
> > NameError: name 'cur' is not defined
>
> All you need to do is make use of the return value. Try this instead:
>
> cur = connect()
>
> That takes the returned cursor object and binds it to the name 'cur'
> in global scope. You can then use 'cur.execute...' and it'll be the
> same object.
>
> As a side point, thank you for posting so clearly. It's easy to help
> when your code and traceback are all there!
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>

You asked this question yesterday.  I answered as did several others.  You
seem to be stuck on how values are returned from a function.

Chris gave you the same help that you got yesterday.

The cur variable within the function disappears when the function ends.
Read about python namespaces to learn more.  So, you need to return the cur
value in your function by using this statement:

return cur

Then when you call connect() is will return the cur value.  You have to
name it like this:

cur = connect()

You should go to python.org and read the tutorials, specifically about
functions.


good luck

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


Re: logging, can one get it to email messages over a certain level?

2012-11-12 Thread tinnews
Peter Otten <__pete...@web.de> wrote:
> tinn...@isbd.co.uk wrote:
> 
> > Steve Howell  wrote:
> >> On Nov 11, 9:48 am, tinn...@isbd.co.uk wrote:
> >> > I'm sure this must be possible but at the moment I can't see how to do
> >> > it.
> >> >
> >> > I want to send an E-Mail when the logging module logs a message above
> >> > a certain level (probably for ERROR and CRITICAL messages only).
> >> >
> >> > I.e. I want some sort of hook that will be called when these messages
> >> > are logged (I can do the sendmail bit OK, I've got code that does
> >> > that).
> >> >
> >> > --
> >> > Chris Green
> >> 
> >> Scroll down to the Handlers section here:
> >> 
> >>http://docs.python.org/2/howto/logging.html#logging-basic-tutorial
> >> 
> >> I'm not sure this explains perfectly what you're gonna need to do, but
> >> I hope it gets you in the ballpark.
> >> 
> > Thank you, but yes I agree it's not terribly informative is it.
> 
> Here's a minimal example:
> 
> import logging
> from logging.handlers import SMTPHandler
> 
> if __name__ == "__main__":
> logging.basicConfig(level=logging.INFO)
> 
> mail_handler = SMTPHandler(
> "smtp.example.com",
> "naviga...@example.com",
> "captain.n...@example.com",
> "fyi")
> mail_handler.setLevel(logging.CRITICAL)
> 
> root = logging.getLogger()
> root.addHandler(mail_handler)
> 
> # will print amessage
> root.warn("this is fishy") 
> 
> # will print a message and send an email
> root.critical("giant squid sighted")
> 
Thank you.

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


Re: List comprehension for testing **params

2012-11-12 Thread Ulrich Eckhardt

Am 11.11.2012 23:24, schrieb Cantabile:

I'm writing a small mail library for my own use, and at the time I'm
testing parameters like this:


Let's ignore the facts that there is an existing mail library, that you 
should use real parameters if they are required and that exit() is 
completely inappropriate. Others explained why sufficiently.


[slightly shortened]

def function(**params)
required = ['Subject', 'From', 'To', 'msg']
for i in required:
if not i in params.keys():
print "Error, \'%s\' argument is missing" %i


Let's start with the last line: If you use "Error, missing {!r} 
argument".format(i), you get the quotes automatically, plus possibly 
escaped unicode characters and newlines, although it's not necessary. 
Also, I would "from __future__ import print_function" in any new code, 
to ease upgrading to Python 3.


Now, concerning the test whether the required parameters are passed to 
the function, you can use "if i in params", which is slightly shorter 
and IMHO similarly expressive. Also, it doesn't repeatedly create a 
list, checks for a single element inside that list and then throws the 
list away again. Further, you don't even need a list for the parameters, 
since order doesn't matter and duplicates shouldn't exist, so I would 
use a set instead:


  required = {'Subject', 'From', 'To', 'msg'}

The advantage is slightly faster lookup (completely irrelevant for this 
small number of elements) but also that it makes the intention a bit 
clearer to people that know what a set is.


In a second step, you compute the intersection between the set of 
required arguments and the set of supplied arguments and verify that all 
are present:


   if required.intersection(params.keys()) != required:
   missing = required - set(params.keys())
   raise Exception("missing arguments {}".format(
   ', '.join(missing)))


Happy hacking!

Uli

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


Re: Is there a way to creat a func that returns a cursor that can be used?

2012-11-12 Thread Chris Angelico
On Mon, Nov 12, 2012 at 9:45 PM, Khalid Al-Ghamdi  wrote:
> Is there a way to create a func that returns a cursor that can be used to 
> execute sql statements?

Yes, and you're almost there!

> I tried this (after importing sqlite3), but it gave me the error below:
>
 def connect():
> return cur
 connect()
> 
 cur.execute("select * from schedule")
> Traceback (most recent call last):
>   File "", line 1, in 
> cur.execute("select * from schedule")
> NameError: name 'cur' is not defined

All you need to do is make use of the return value. Try this instead:

cur = connect()

That takes the returned cursor object and binds it to the name 'cur'
in global scope. You can then use 'cur.execute...' and it'll be the
same object.

As a side point, thank you for posting so clearly. It's easy to help
when your code and traceback are all there!

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


Re: Is there a way to creat a func that returns a cursor that can be used?

2012-11-12 Thread Peter Otten
Khalid Al-Ghamdi wrote:

> Is there a way to create a func that returns a cursor that can be used to
> execute sql statements?

You should read an introductory text on Python, this is not specific to 
sqlite3.
 
> I tried this (after importing sqlite3), but it gave me the error below:
> 
 def connect():
> conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db   etc.
> cur = conn.cursor()
> cur.execute("create table schedule (teams integer, sn integer, badge
> integer ,name text, grp integer,\
> major text, track text, stage text,  tc text, subject text, course
> text, ws text, date text, \ time text, proctor text, code text, no
> integer,fl_time text, flag2 text, flag3 text, flag4 text, clash1
> integer, clash2 integer)") return cur
 connect()

connect() returns a cursor, but you discard the result. Try

 cur = connect()

> 
 cur.execute("select * from schedule")
> Traceback (most recent call last):
>   File "", line 1, in 
> cur.execute("select * from schedule")
> NameError: name 'cur' is not defined


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


Re: logging, can one get it to email messages over a certain level?

2012-11-12 Thread Peter Otten
tinn...@isbd.co.uk wrote:

> Steve Howell  wrote:
>> On Nov 11, 9:48 am, tinn...@isbd.co.uk wrote:
>> > I'm sure this must be possible but at the moment I can't see how to do
>> > it.
>> >
>> > I want to send an E-Mail when the logging module logs a message above
>> > a certain level (probably for ERROR and CRITICAL messages only).
>> >
>> > I.e. I want some sort of hook that will be called when these messages
>> > are logged (I can do the sendmail bit OK, I've got code that does
>> > that).
>> >
>> > --
>> > Chris Green
>> 
>> Scroll down to the Handlers section here:
>> 
>>http://docs.python.org/2/howto/logging.html#logging-basic-tutorial
>> 
>> I'm not sure this explains perfectly what you're gonna need to do, but
>> I hope it gets you in the ballpark.
>> 
> Thank you, but yes I agree it's not terribly informative is it.

Here's a minimal example:

import logging
from logging.handlers import SMTPHandler

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)

mail_handler = SMTPHandler(
"smtp.example.com",
"naviga...@example.com",
"captain.n...@example.com",
"fyi")
mail_handler.setLevel(logging.CRITICAL)

root = logging.getLogger()
root.addHandler(mail_handler)

# will print amessage
root.warn("this is fishy") 

# will print a message and send an email
root.critical("giant squid sighted")


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


Is there a way to creat a func that returns a cursor that can be used?

2012-11-12 Thread Khalid Al-Ghamdi
Is there a way to create a func that returns a cursor that can be used to 
execute sql statements? 

I tried this (after importing sqlite3), but it gave me the error below:

>>> def connect():
conn = sqlite3.connect(':memory:')#use sch3.db or sch4.db   etc.
cur = conn.cursor()
cur.execute("create table schedule (teams integer, sn integer, badge 
integer ,name text, grp integer,\
major text, track text, stage text,  tc text, subject text, course text, ws 
text, date text, \
time text, proctor text, code text, no integer,fl_time text, flag2 text, 
flag3 text, flag4 text, clash1 integer, clash2 integer)")
return cur
>>> connect()

>>> cur.execute("select * from schedule")
Traceback (most recent call last):
  File "", line 1, in 
cur.execute("select * from schedule")
NameError: name 'cur' is not defined
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: skip Trackback error for ftp checking

2012-11-12 Thread Chris Angelico
On Mon, Nov 12, 2012 at 8:25 PM, moonhkt  wrote:
> On Nov 10, 2:50 pm, Steven D'Aprano  +comp.lang.pyt...@pearwood.info> wrote:
>>
>> The same way you would skip any other error when you do something wrong:
>> catch the exception.
>
> Thank. Added below.
> try:
>ftp = FTP(options.remote_host_address)
> except :
>   print "Host address not found."
>   sys.exit(1)

Heh, I'm afraid that's not quite what Steven meant; what you're doing
there is literally skipping _any other error_. A bare except is
usually not a good thing. Catch the specific exception you want to
catch - that way, if you typo a name or something, you don't get an
obscure message about the host address when it's really a coding bug.

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


Re: logging, can one get it to email messages over a certain level?

2012-11-12 Thread tinnews
Steve Howell  wrote:
> On Nov 11, 9:48 am, tinn...@isbd.co.uk wrote:
> > I'm sure this must be possible but at the moment I can't see how to do it.
> >
> > I want to send an E-Mail when the logging module logs a message above
> > a certain level (probably for ERROR and CRITICAL messages only).
> >
> > I.e. I want some sort of hook that will be called when these messages
> > are logged (I can do the sendmail bit OK, I've got code that does that).
> >
> > --
> > Chris Green
> 
> Scroll down to the Handlers section here:
> 
>http://docs.python.org/2/howto/logging.html#logging-basic-tutorial
> 
> I'm not sure this explains perfectly what you're gonna need to do, but
> I hope it gets you in the ballpark.
> 
Thank you, but yes I agree it's not terribly informative is it.

However I think I understand what I need to do.  I currently have a
function to set up my logging:-

def initLog(name):
log = logging.getLogger(name)
log.setLevel(logging.DEBUG)
f = logging.handlers.RotatingFileHandler("/home/chris/tmp/mail.log", 
'a', 100, 4)
f.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - 
%(message)s')
f.setFormatter(formatter)
log.addHandler(f)
return log

As I understand it I just add another line like the 'f =' line but
using the SMTPHandler and then set an appropriate level for that
handler (and formatting).


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


Fwd: error

2012-11-12 Thread inshu chauhan
No answers for my question ?? :O
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: skip Trackback error for ftp checking

2012-11-12 Thread moonhkt
On Nov 10, 2:50 pm, Steven D'Aprano  wrote:
> On Fri, 09 Nov 2012 20:51:47 -0800, moonhkt wrote:
> > HI All
>
> > How to skip Trackback warning/error when input ftp address is not
> > correct or reject ?
>
> The same way you would skip any other error when you do something wrong:
> catch the exception.
>
> --
> Steven

Thank. Added below.
try:
   ftp = FTP(options.remote_host_address)
except :
  print "Host address not found."
  sys.exit(1)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: duck typing assert

2012-11-12 Thread Ian Kelly
On Fri, Nov 9, 2012 at 6:36 AM, Steven D'Aprano
 wrote:
> (I think... I really don't actually know if Zooey Deschanel can sing or
> not. Just go along with the example.)

Not only does she sing, she's in a band.

http://en.wikipedia.org/wiki/She_%26_Him

I take your point about the "looks like" terminology, though.
-- 
http://mail.python.org/mailman/listinfo/python-list