Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Antoon Pardon
Op 26-11-13 22:42, Tim Delaney schreef:
> On 27 November 2013 03:57, Antoon Pardon  > wrote:
> 
> 
> So I can now ask my questions in dutch and expect others to try and
> understand me instead of me asking them in english? Or can I use
> literal translations of dutch idioms even if I suspect that such
> a literal translation could be misunderstood and even be insulting?
> 
> 
> 1. No, because this is stated to be an English-speaking list/newsgroup.
> It just doesn't specify what dialect of English.

Well so much for this group being an international group with only one
language allowed.

However that second sentence doesn't make much sense to me. Modern
languages contain a subset that is called the standard language. This
is the subset that is generally taught. Especially to those for whom
the language is foreign. So when you define a specific language to
use on an international forum, it is strongly suggested that people
limit themselves to the standard subset and don't use dialects since
"dialect" AFAIU means it is outside this standard.

Yes I accept that everyone deviates from this standard language and that
it isn't always easy to know what is and what is not within the standard
language and that we should allow each other some leeway. However there
is a difference between saying standard usage is something to aspire to
and then be tolerant for deviations on the one hand and saying any
dialect is allowed on the other hand.

So this being an international forum in which for a significant number
of members english is not their first language, I think it would be
prudent for those who have englisch as a mother tongue, to try and
stick to standard english, so as not to burden the first group even
more.

Doing otherwise IMO doesn't show much respect for that first group, from
whom is expected they adapt to a (for them) foreign language and then to
learn that those for which english is their mother tongue don't feel an
obligation to be helpful by limiting themselves to that part of the
language that is most likely to be understood by the first group.

> Participants are expected to attempt to be understandable in English,
> but I personally expect responders to make an effort to work with
> multiple dialects.

Why do you expect from people who already had to learn a foreign
language to familiarize themselves with dialects. The variations
within the standard are already plentyful enough, that you shouldn't
burden these peoples with dialects too.

You seem to suggest that we can hardly expect from people for whom
english is their mother tongue to do a serious effort in making
themselves understandable to others by trying to express themselves
in standard english.

And that in what is accepted to be an international forum so in which
we can expect a significant number of people for whom english is not
their mother tongue.

-- 
Antoon Pardon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Chris Angelico
On Wed, Nov 27, 2013 at 7:16 PM, Antoon Pardon
 wrote:
> However that second sentence doesn't make much sense to me. Modern
> languages contain a subset that is called the standard language. This
> is the subset that is generally taught. Especially to those for whom
> the language is foreign. So when you define a specific language to
> use on an international forum, it is strongly suggested that people
> limit themselves to the standard subset and don't use dialects since
> "dialect" AFAIU means it is outside this standard.

Do you mean standard British English, standard American English,
standard Australian English, or some other?

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Antoon Pardon
Op 27-11-13 09:19, Chris Angelico schreef:
> On Wed, Nov 27, 2013 at 7:16 PM, Antoon Pardon
>  wrote:
>> However that second sentence doesn't make much sense to me. Modern
>> languages contain a subset that is called the standard language. This
>> is the subset that is generally taught. Especially to those for whom
>> the language is foreign. So when you define a specific language to
>> use on an international forum, it is strongly suggested that people
>> limit themselves to the standard subset and don't use dialects since
>> "dialect" AFAIU means it is outside this standard.
> 
> Do you mean standard British English, standard American English,
> standard Australian English, or some other?

Does that significantly matter or are you just looking for details
you can use to disagree? As far as I understand the overlap between
standard British English and standard American English is so large
that it doesn't really matter for those who had to learn the language.
Likewise for the overlap with standard Australian English.

-- 
Antoon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Chris Angelico
On Wed, Nov 27, 2013 at 7:31 PM, Antoon Pardon
 wrote:
> Op 27-11-13 09:19, Chris Angelico schreef:
>> On Wed, Nov 27, 2013 at 7:16 PM, Antoon Pardon
>>  wrote:
>>> However that second sentence doesn't make much sense to me. Modern
>>> languages contain a subset that is called the standard language. This
>>> is the subset that is generally taught. Especially to those for whom
>>> the language is foreign. So when you define a specific language to
>>> use on an international forum, it is strongly suggested that people
>>> limit themselves to the standard subset and don't use dialects since
>>> "dialect" AFAIU means it is outside this standard.
>>
>> Do you mean standard British English, standard American English,
>> standard Australian English, or some other?
>
> Does that significantly matter or are you just looking for details
> you can use to disagree? As far as I understand the overlap between
> standard British English and standard American English is so large
> that it doesn't really matter for those who had to learn the language.
> Likewise for the overlap with standard Australian English.

It matters hugely when your point depends on their being a single
"standard English". The overlap may be large, but all you've done is
either change the terms without solving the problem (because there are
still multiple language variants being used) or create a new language
(the common subset of English across all usages, which is an
impossible target to aim for).

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Antoon Pardon
Op 27-11-13 09:36, Chris Angelico schreef:
> On Wed, Nov 27, 2013 at 7:31 PM, Antoon Pardon
>  wrote:
>> Op 27-11-13 09:19, Chris Angelico schreef:
>>> On Wed, Nov 27, 2013 at 7:16 PM, Antoon Pardon
>>>  wrote:
 However that second sentence doesn't make much sense to me. Modern
 languages contain a subset that is called the standard language. This
 is the subset that is generally taught. Especially to those for whom
 the language is foreign. So when you define a specific language to
 use on an international forum, it is strongly suggested that people
 limit themselves to the standard subset and don't use dialects since
 "dialect" AFAIU means it is outside this standard.
>>>
>>> Do you mean standard British English, standard American English,
>>> standard Australian English, or some other?
>>
>> Does that significantly matter or are you just looking for details
>> you can use to disagree? As far as I understand the overlap between
>> standard British English and standard American English is so large
>> that it doesn't really matter for those who had to learn the language.
>> Likewise for the overlap with standard Australian English.
> 
> It matters hugely when your point depends on their being a single
> "standard English". The overlap may be large, but all you've done is
> either change the terms without solving the problem (because there are
> still multiple language variants being used) or create a new language
> (the common subset of English across all usages, which is an
> impossible target to aim for).

You are nitpicking. I didn't say nor implied their is a perfect
solution. However that there is no perfect solution doesn't imply
we can't expect some effort from those with english as a mother
tongue to search for ways in which to express themselves that are
more likely to be understood by those who had to learn english
as a foreign language than just to use their local idiom/dialect.

I think that is basic respect for those who had to learn the language.

-- 
Antoon Pardon

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


Re: Python application for rpm creation

2013-11-27 Thread Matthias Runge
On 11/27/2013 03:28 AM, Amit Saha wrote:
> On Wed, Nov 27, 2013 at 1:39 AM, Unix SA  wrote:
>>
>>> Sounds to me more like he is looking to package some other in house
>>> software, as opposed to packaging python specific libraries, etc..
>>
>> - Yes, This is exactly i am looking at
>>
>>
>>> Doing an apt-cache search on my Ubuntu desktop results with a project,
>>> Spectacle, coincidentally written in Python. (I haven't really looked > into
>>> it):
>>> http://meego.gitorious.org/meego-developer-tools/spectacle
>>
>> this looks useful, i shall looking to this... or may be try myself writing
>> something.
>>
>> if you guys ( others ) got something else for Redhat Linux rpm creation do
>> let me know.
> 
> I played with creating a RPM SPEC file "generator" for Sphinx documentation:
> https://github.com/amitsaha/sphinx_doc_packaging
> 
> It's written in Python, so perhaps may help with you a starting point.
> 
> Best,
> Amit.
> 
> 
In Fedora (and IMHO in EPEL, too) there is a package named pyp2rpm. This
is quite handy. It fetches sources from pypi, writes a basic SPEC file,
which might need minor tweaks, but in general, it really saves you time.

Matthias
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 7:41:54 AM UTC+5:30, Ned Batchelder wrote:
> On 11/26/13 8:26 PM, Rick Johnson wrote:



> And will you be here to explain to time-travelling Shakespeare why we 
> are all of us speaking English completely wrong (to his ears)?

And to my (Indian!!) ears when Tim says 'plank in the eye' where King James
says 'beam' it does not cut it.

Propositionally: Its a distinction without a difference
Poetically: Well its subjective... to me its a real difference

Likewise in programming:
Propositionally: All languages are equal -- Turing complete -- and people
discussing/inventing new ones are just wasting their and others' time

Poetically: Like all artistic questions this is not settle-able once
and for all and I must preface the following with an "I find that..."
C is artistic in a very different way from Python and assembly
and Haskell.  And C++ is frighteningly unartistic

It is my impression that the arguments that happen in/around programming 
languages are more-heat-less-light than in typical art/science because artistic 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 3:02:54 PM UTC+5:30, rusi wrote:

[Last line cut-off by mistake!]

> It  is  my  impression  that the  arguments  that  happen  in/around
> programming  languages  are  more-heat-less-light  than  in  typical
> art/science because artistic questions masquerade as scientific ones

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


Re: Still off topic. Deal with it. [was Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !]

2013-11-27 Thread Jussi Piitulainen
Steven D'Aprano writes:

> On Tue, 26 Nov 2013 20:24:16 -0800, Rick Johnson wrote:
> 
> > Although i would strongly prefer for him to choose ubiquitous
> > definitions *over* regional definitions when posting to internet
> > forums, i would have happily ignored this thread had it not been
> > for Stevens emotional plea of:
> > 
> > "As this is an international forum, it behoves [sic] us all to
> > make allowances for slight difference in dialect."
> 
> Please don't mislabel my correctly spelt words with "sic" just
> because your regional variation of the Queens English uses a
> different spelling.

Don't you know POTUS's Kenyan? [/joke]

(Well done about "behove". I knew the word is FORMAL[*] but I had no
idea that the variant spelling I knew was a regionalism. "Queen's",
though. :)

[*] Not shouting. Imitating the small caps that COBUILD uses for such
labels.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Completely and utterly Off Topic [was Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !]

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 12:18:11 PM UTC+5:30, Steven D'Aprano wrote:
> On Tue, 26 Nov 2013 17:26:48 -0800, Rick Johnson wrote:

> [...]
> >  "A new home-run record!"

> What is this "home-run" of which you speak? Houses don't generally run. 
> Surely you're not using a regional idiom outside of your region?

Six!

[If you are Brit/Aussie/Indian, you'd get that
 If not you probably dont
 If not and you do then you are a true cosmopolite
]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Completely and utterly Off Topic [was Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !]

2013-11-27 Thread Chris Angelico
On Wed, Nov 27, 2013 at 8:47 PM, rusi  wrote:
> On Wednesday, November 27, 2013 12:18:11 PM UTC+5:30, Steven D'Aprano wrote:
>> On Tue, 26 Nov 2013 17:26:48 -0800, Rick Johnson wrote:
>
>> [...]
>> >  "A new home-run record!"
>
>> What is this "home-run" of which you speak? Houses don't generally run.
>> Surely you're not using a regional idiom outside of your region?
>
> Six!
>
> [If you are Brit/Aussie/Indian, you'd get that
>  If not you probably dont
>  If not and you do then you are a true cosmopolite
> ]

That's better than "Six and out, and go find it", which is what
happens if you put it over a fence or in any inaccessible place.

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


Re: Completely and utterly Off Topic [was Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !]

2013-11-27 Thread Mark Lawrence

On 27/11/2013 06:48, Steven D'Aprano wrote:

On Tue, 26 Nov 2013 17:26:48 -0800, Rick Johnson wrote:


Even if you are correct that the OP is using a regional variation of
English, you fail to realize that this "regional redefinition" of the
English word: "doubts" to mean what the *majority* of  English speaking
world understands as "questions", cannot be justified OUTSIDE of his
region.


"Fail to realize"? What regional redefinition of "realise" is that? How
do you justify using that regional variation outside of your region?


Had a really good chuckle over this, thanks.





It's not like he's using a NEW word; a word that has never been defined,
NO, his region has redefined a widely understood word.

[...]

In the previous examples we show that introducing a NEW word is fine,
because, at least when we encounter a NEW word we will *instantly* know
that we need to find a definition for the NEW word BEFORE we can *fully*
comprehend what the author is trying to tell us.


I completely sniglim with what you are saying. I'd go further and state
that, without exception, your argument is the most vumtigious I've ever
seen, and if there were any justice in the world, people would follow you
down the street shouting "Gedus! Gedus!" and giving you a keddener. If
anyone deserves it, it is you.



Don't have time now but search engine will be busy later, can't let 
these things pass unchequed.




[...]

When we see the word "doubts", followed by an enumerated listing, we
falsely believe the lad


"The lad"? Well, I suppose that's a step up from calling men twice your
age "boy", but not much.



is confused or has some level of concern.


Whereas when somebody says they have a question, we immediately assume
that they are not confused, and have no concern at all.


[...]

Now... *hopefully* we can understand why the words "question" and
"doubt" should NEVER be used interchangeably.


Now Rick, I know that you're a speaker of a regional variation of
English, so you might not be familiar with the standard meanings of the
word "doubt" in English, including:

 3.  A point about which one is uncertain or skeptical: reassured
 me by answering my doubts.

http://www.thefreedictionary.com/doubt


It is without doubt that "question" and "doubt" are synonyms, or perhaps
I should say that it is without question that "doubt" and "question" are
synonyms.

http://thesaurus.com/browse/doubt

Of course, if you have any doubts about this, feel free to ask, we're
happy to answer all reasonable questions.


[...]

  "A new home-run record!"


What is this "home-run" of which you speak? Houses don't generally run.
Surely you're not using a regional idiom outside of your region?



I believe that he's referring to the need to rush home in order to use 
the toilet, water closet or whatever your dialect uses.  Apparently in 
some parts of the world a guy called John is constantly being urinated 
and defecated on, I'll admit to feeling really sorry for him.


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Mark Lawrence

On 27/11/2013 08:16, Antoon Pardon wrote:

Op 26-11-13 22:42, Tim Delaney schreef:

On 27 November 2013 03:57, Antoon Pardon mailto:antoon.par...@rece.vub.ac.be>> wrote:


 So I can now ask my questions in dutch and expect others to try and
 understand me instead of me asking them in english? Or can I use
 literal translations of dutch idioms even if I suspect that such
 a literal translation could be misunderstood and even be insulting?


1. No, because this is stated to be an English-speaking list/newsgroup.
It just doesn't specify what dialect of English.


Well so much for this group being an international group with only one
language allowed.

However that second sentence doesn't make much sense to me. Modern
languages contain a subset that is called the standard language. This
is the subset that is generally taught. Especially to those for whom
the language is foreign. So when you define a specific language to
use on an international forum, it is strongly suggested that people
limit themselves to the standard subset and don't use dialects since
"dialect" AFAIU means it is outside this standard.

Yes I accept that everyone deviates from this standard language and that
it isn't always easy to know what is and what is not within the standard
language and that we should allow each other some leeway. However there
is a difference between saying standard usage is something to aspire to
and then be tolerant for deviations on the one hand and saying any
dialect is allowed on the other hand.

So this being an international forum in which for a significant number
of members english is not their first language, I think it would be
prudent for those who have englisch as a mother tongue, to try and
stick to standard english, so as not to burden the first group even
more.

Doing otherwise IMO doesn't show much respect for that first group, from
whom is expected they adapt to a (for them) foreign language and then to
learn that those for which english is their mother tongue don't feel an
obligation to be helpful by limiting themselves to that part of the
language that is most likely to be understood by the first group.


Participants are expected to attempt to be understandable in English,
but I personally expect responders to make an effort to work with
multiple dialects.


Why do you expect from people who already had to learn a foreign
language to familiarize themselves with dialects. The variations
within the standard are already plentyful enough, that you shouldn't
burden these peoples with dialects too.

You seem to suggest that we can hardly expect from people for whom
english is their mother tongue to do a serious effort in making
themselves understandable to others by trying to express themselves
in standard english.

And that in what is accepted to be an international forum so in which
we can expect a significant number of people for whom english is not
their mother tongue.



You're in cloud cuckoo land.  And it's English, thank you very much.

--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Recursive generator for combinations of a multiset?

2013-11-27 Thread John O'Hagan
On Tue, 26 Nov 2013 10:33:06 +
Oscar Benjamin  wrote:

> On 26 November 2013 06:18, John O'Hagan 
> wrote:
> >
[...]
> >
> > def _multicombs(prepend, words, r, chkstr):
> > """chkstr is the string of remaining availalable characters"""
> > if r == 0:
> > yield prepend, chkstr
> > return
> > (word, count, rem), *remaining = words
> > newstr = chkstr
> > for k in range(max(r-rem, 0), min(count, r) + 1):
> > for letter in word * k:
> > if letter in newstr:
> > newstr = newstr.replace(letter, '' , 1)
> > else:
> > return
> > yield from _multicombs(prepend + (word,) * k, remaining,
> > r-k, newstr)
> 
> Further micro-optimisations are possible. One is to inline the lower
> recursion levels. For example if the termination condition is checked
> immediately before recursion you can eliminate a redundant generator
> function call.
> 
> > By progressively checking against the available characters, it
> > avoids fruitless recursion branches. I'm not 100% sure I'm doing
> > this right yet but so far it's promising. This is the quickest
> > non-redundant approach I've used so far (although strangely, the
> > original product-only version which produces a lot of redundant
> > results is still by far the quickest.)
> 
> Bear in mind that the interpreter does a lot of  "redundant" things so
> what you might think of as non-redundant code can actually be highly
> redundant when interpreter over-head is accounted for.

I don't mind redundancy as long as I don't have to see it in the output!

> Recently I've been trying out PyPY in my own work and it is *really
> fast*. In fact it is so much faster that I've given up on the idea of
> speed-optimising code for CPython: just use PyPy instead - although it
> is worth optimising highly intensive code for PyPy.

I'll have a look at it.

> > I'll try to explain better what I'm actually doing. It's quite long,
> > but you seem to be wondering, or maybe someone else is!
> 
> [snip]
> 
> Okay I understand now. I was confused because I think of anagrams as
> being words of the same length. I didn't understand how your multiword
> version works but I think I do now. In my own words: You want to
> generate all sequences of English words having the same letters as
> some input string, ignoring the spaces in both the input and output
> strings (so that the number or length of the individual words need not
> be the same).
> 
> [snip]
> >
> > For example, take the phrase "break beat". I make a wordlength
> > dictionary of sub-alphabet words, using the system dictionary file,
> > like this:
> >
> >  {1: ['a'], 2: ['at', 'be', 're'], 3: ['are', 'ark', 'art', 'ate',
> >  'baa', 'bar', 'bat', 'bee', 'bet', 'bra', 'ear', 'eat', 'ebb',
> > 'eke', 'era', 'ere', 'eta', 'rat', 'tab', 'tar', 'tea', 'tee'], 4:
> > ['abet', 'area', 'babe', 'bake', 'barb', 'bare', 'bark', 'bate',
> > 'beak', 'bear', 'beat', 'beer', 'beet', 'beta', 'brat', 'rake',
> > 'rate', 'reek', 'take', 'tare', 'teak', 'tear', 'tree', 'trek'], 5:
> > ['abate', 'baker', 'beret', 'brake', 'break', 'eater', 'karat',
> > 'kebab', 'taker'], 6: ['aerate', 'beaker', 'beater', 'berate',
> > 'betake', 'karate', 'rebate', 'retake']}
> 
> Okay, how about the following (pseudoish-code)?
> 
> def word_sequences(prepend, target, subwords):
> # subwords is a list rather than a dict
> for n in range(1, len(subwords)):
> for word in subwords[n]:
> if equal_in_a_multiset_sense(word, target):
> yield prepend + ' ' + target
> elif is_a_submultiset(word, target):
> recurse_target = multiset_subtract(target, word)
> subsubwords =
> list_of_subwords_by_length[:len(recurse_target)] if any(subsubwords):
> yield from word_sequences(prepend + ' ' + word,
> recurse_target, subsubwords)
> 

[...]

That is very clever. Direct, economical and does effectively the
same pruning job as my approach without all the combinatorial
brainstrain. I got it working by changing "for n in range..." to "for
words in subwords" (otherwise it missed the one-letter words) and the
first yield needed to be "prepend + ' ' + word". 

I simplified it a bit more to this:

def word_sequences(prepend, target, subwords):
"""subwords is a list of lists of subwords grouped by length,
in order of length"""
for words in subwords:
for word in words:
recurse_target = subset_subtract(target, word)
if recurse_target:
yield from word_sequences(prepend + ' ' + word,
recurse_target, subwords[:len(recurse_target)])
elif recurse_target == '':
yield prepend + ' ' + word

with just one function to do the subset testing:

def subset_subtract(target, word):
for i in word:
if i in target:
target = target.replace(i, '' ,1)
else:
return

Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Ned Batchelder

On 11/26/13 11:24 PM, Rick Johnson wrote:

On Tuesday, November 26, 2013 8:11:54 PM UTC-6, Ned Batchelder wrote:


Rick, through all the verbiage, I've lost track of what you are
advocating.  The OP asks a question and uses the word doubt in a way
that is unusual to you and many other, though not unusual where he is
from.  What is it you want us to do?
Why not just relax and answer his question and leave it at that?


Hello again Ned. I now understand why you're confused.
Please allow me to explain.

>
> (16 paragraphs snipped)
>

Rick, I am confused because you have taken a direct question, "what is 
it you want us to do", and haven't actually answered it.  You've spoken 
strongly, you've made elaborate but baffling analogies to squirrels, 
you've given us pop quizzes, you've created mock dialogs with others, 
etc.  Where is the answer to the question?  Speak directly and plainly, 
and maybe I will understand.


The OP asked a question using the word "codes" for "program".  (This was 
actually the word that got us started, not "doubt", I had to look back 
to realize.)  A number of people tried to answer the question.  Denis 
made a joke, which often happens on the list, based on the multiple 
meanings of "codes".  Stephen said we should make allowances for slight 
difference in dialect.


What parts here are you objecting to?

I don't know what course of action you are advocating.  Can you explain, 
briefly, without resorting to analogies, quizzes, or philosophers?  What 
do you want people in this forum to do?


It could be that you are fine with the way things are going in this 
forum, and are only objecting to a fine point of Steven's plea.  If so, 
then maybe we could just drop it as off-topic.


--Ned.


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


Wrapping around a list

2013-11-27 Thread amjadcsu
Hello,
I am working on a problem (Bioinformatics domain) where all possible 
combinations of input string needs to be printed as sublist

For example:
Input string : "LEQN"
Output= "[L","E","Q","N"]["LE","EQ","QN","NL"] ["LEQ","EQN","QNE","NLE"]
["LEQN"]

The code i have written for this is as follows:

from itertools import chain, repeat, islice
from collections import deque

def sliding_window(iterable, n, fill=False, fillvalue=None):
it = iter(iterable)
if fill:
it = chain(it, repeat(fillvalue, n - 1))
w = deque(islice(it, n - 1))
for x in it:
w.append(x)
yield w
w.popleft()

input="LENQ"

lstinput= list(input)
lenlstinput=len(lstinput)
list1=[''.join(x) for x in sliding_window(lstinput, 2)]
list2= [''.join(x) for x  in sliding_window(lstinput, 3)]
list3= [''.join(x) for x in sliding_window(lstinput, 4)]



The output i get as follows:
List 1 is  ['LE', 'EN', 'NQ']   Should be ['LE','EN','NQ','QL']
 List 2 is  ['LEN', 'ENQ']  Should be ['LEN','ENQ','NQL','QLE']

So the question i am asking , how can i add wrapping around sublist in my 
sliding window function.

Thanks 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Wrapping around a list

2013-11-27 Thread Chris Angelico
On Wed, Nov 27, 2013 at 9:46 PM,   wrote:
> So the question i am asking , how can i add wrapping around sublist in my 
> sliding window function.

By the look of what you now have, the easiest way would probably be to
duplicate the list before you slide. So instead of working on "LEQN",
you work on "LEQNLEQN" - or possibly "LEQNLEQ", or other shortened
versions, if it's easier than pulling out the duplicates.

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Paul Rudin
rusi  writes:

> Propositionally: All languages are equal -- Turing complete 

As an aside, not all languages are Turing complete. For example Charity
is a language with the property that programs are guaranteed to
terminate.



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


Re: Wrapping around a list

2013-11-27 Thread Amjad Syed
On Wednesday, November 27, 2013 1:53:54 PM UTC+3, Chris Angelico wrote:
> On Wed, Nov 27, 2013 at 9:46 PM,   wrote:
> 
> > So the question i am asking , how can i add wrapping around sublist in my 
> > sliding window function.
> 
> 
> 
> By the look of what you now have, the easiest way would probably be to
> 
> duplicate the list before you slide. So instead of working on "LEQN",
> 
> you work on "LEQNLEQN" - or possibly "LEQNLEQ", or other shortened
> 
> versions, if it's easier than pulling out the duplicates.
> 
> 
> 
> ChrisA


Thanks Chris for the reply. But i would like sliding function to be scalable, 
as input string can be of 100 letters.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Wrapping around a list

2013-11-27 Thread Chris Angelico
On Wed, Nov 27, 2013 at 10:07 PM, Amjad Syed  wrote:
> Thanks Chris for the reply. But i would like sliding function to be scalable, 
> as input string can be of 100 letters.

A hundred isn't much to work with, and your code will be fairly
simple. Give it a try with small strings, see how it goes; then try it
on your full-size - I doubt there'll be a problem.

Now, if you were talking about a hundred million letters, then maybe
there'd be a problem. But even there, I'd start with the simple and
clean approach, and only optimize for performance when it becomes
obviously necessary (like when my MUD client was able to saturate one
core of my CPU, just by me typing a lot of commands very rapidly -
that needed fixing!).

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


Re: Wrapping around a list

2013-11-27 Thread Ned Batchelder

On 11/27/13 6:14 AM, Chris Angelico wrote:

On Wed, Nov 27, 2013 at 10:07 PM, Amjad Syed  wrote:

Thanks Chris for the reply. But i would like sliding function to be scalable, 
as input string can be of 100 letters.


A hundred isn't much to work with, and your code will be fairly
simple. Give it a try with small strings, see how it goes; then try it
on your full-size - I doubt there'll be a problem.

Now, if you were talking about a hundred million letters, then maybe
there'd be a problem. But even there, I'd start with the simple and
clean approach, and only optimize for performance when it becomes
obviously necessary (like when my MUD client was able to saturate one
core of my CPU, just by me typing a lot of commands very rapidly -
that needed fixing!).

ChrisA



Using ChrisA's idea:

def sliding_window(iterable, n, fill=None):
values = list(iterable)
num_values = len(values)
if fill is not None:
values.extend([fill]*(n-1))
need_more = (2*num_values-1) - len(values)
values.extend(values[:need_more])
for start in range(num_values):
yield values[start:start+n]

l = list("LEQNABC")
for n in range(2, len(l)+1):
print [''.join(x) for x in sliding_window(l, n)]
for n in range(2, len(l)+1):
print [''.join(x) for x in sliding_window(l, n, fill="_")]

Produces:

['LE', 'EQ', 'QN', 'NA', 'AB', 'BC', 'CL']
['LEQ', 'EQN', 'QNA', 'NAB', 'ABC', 'BCL', 'CLE']
['LEQN', 'EQNA', 'QNAB', 'NABC', 'ABCL', 'BCLE', 'CLEQ']
['LEQNA', 'EQNAB', 'QNABC', 'NABCL', 'ABCLE', 'BCLEQ', 'CLEQN']
['LEQNAB', 'EQNABC', 'QNABCL', 'NABCLE', 'ABCLEQ', 'BCLEQN', 'CLEQNA']
['LEQNABC', 'EQNABCL', 'QNABCLE', 'NABCLEQ', 'ABCLEQN', 'BCLEQNA', 
'CLEQNAB']

['LE', 'EQ', 'QN', 'NA', 'AB', 'BC', 'C_']
['LEQ', 'EQN', 'QNA', 'NAB', 'ABC', 'BC_', 'C__']
['LEQN', 'EQNA', 'QNAB', 'NABC', 'ABC_', 'BC__', 'C___']
['LEQNA', 'EQNAB', 'QNABC', 'NABC_', 'ABC__', 'BC___', 'C']
['LEQNAB', 'EQNABC', 'QNABC_', 'NABC__', 'ABC___', 'BC', 'C_']
['LEQNABC', 'EQNABC_', 'QNABC__', 'NABC___', 'ABC', 'BC_', 
'C__']


100 elements is really nothing, don't try to over-optimize it.  And if 
your inputs are really strings, not general iterables, you can use the 
same logic but with string operations, and you'll likely have better 
performance anyway.  Less general, true, but better for your actual problem.


--Ned.

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


tkinter bug on mac maverick python 3.3.3

2013-11-27 Thread Dan Wissme

Hi !
Am I the only one to get a bug in GUIs using tkinter on my Mac under 
maverick and Python 3.3.3 ?
When will they get rid of Tcl/Tk which causes recurrent problems at 
almost each new Python version !

Please, for the rest of us...

   -dan
--
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter bug on mac maverick python 3.3.3

2013-11-27 Thread Ned Batchelder

On 11/27/13 6:32 AM, Dan Wissme wrote:

Hi !
Am I the only one to get a bug in GUIs using tkinter on my Mac under
maverick and Python 3.3.3 ?
When will they get rid of Tcl/Tk which causes recurrent problems at
almost each new Python version !
Please, for the rest of us...

-dan


It sounds like you're really frustrated, I'm sorry.  I don't know if you 
want help with your actual bug, if so, provide details.  If it's any 
consolation, Guido himself is also frustrated: 
https://twitter.com/gvanrossum/status/404768253282484224


--Ned.

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


RE: UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

2013-11-27 Thread Hoàng Tuấn Việt
Hi Fabio,

 

I cannot see your reply in python-list until I search this question on Internet 
again.

 

I try (username.encode('utf-8') + '\r') and the problem is fixed.

 

Thank you very much.

 

Viet

 

>You should be able to reproduce the same behavior on PyDev if in your run 
>configuration you select the encoding of the console to be ascii (run > run 
>configurations > select run configuration > common > set >encoding to 
>us-ascii).

> 

>My guess is that you have the problem because the username has non-ascii chars 
>-- and you're receiving it as an unicode and not a string... so, you have to 
>do encode it properly to a string before writing to the >connection (i.e.: 
>username.encode('utf-8') + '\r' -- although the encoding may have to be a 
>different one and not utf-8).

> 

>Cheers,

> 

>Fabio

 

 

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


Re: UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

2013-11-27 Thread Tim Golden
On 27/11/2013 08:31, Hoàng Tuấn Việt wrote:
> I cannot see your reply in python-list until I search this question
> on Internet again.

(cc-ing the OP because of the nature of the problem)

Viet,

That's because you're not subscribed to the list. The way it works is
that you subscribe to the list and all your posts go to everyone else
who's subscribed and you see all their posts. At present, you're
operating as a "guest" which means that one of us has to approve every
post you make.

It looks as though Fabio's solved your current problem, but please try
subscribing if you want to post again:

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

Thanks

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Robert Kern

On 2013-11-27 08:31, Antoon Pardon wrote:

Op 27-11-13 09:19, Chris Angelico schreef:

On Wed, Nov 27, 2013 at 7:16 PM, Antoon Pardon
 wrote:

However that second sentence doesn't make much sense to me. Modern
languages contain a subset that is called the standard language. This
is the subset that is generally taught. Especially to those for whom
the language is foreign. So when you define a specific language to
use on an international forum, it is strongly suggested that people
limit themselves to the standard subset and don't use dialects since
"dialect" AFAIU means it is outside this standard.


Do you mean standard British English, standard American English,
standard Australian English, or some other?


Does that significantly matter or are you just looking for details
you can use to disagree? As far as I understand the overlap between
standard British English and standard American English is so large
that it doesn't really matter for those who had to learn the language.
Likewise for the overlap with standard Australian English.


Since the original usage that you are complaining about is "standard" Indian 
English[1], yes, it does significantly matter.


[1] To the extent that there is such a thing as a "standard" form of any 
language. Which there isn't, but I will grant you your premise for the time being.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Robert Kern

On 2013-11-27 08:16, Antoon Pardon wrote:

Op 26-11-13 22:42, Tim Delaney schreef:

On 27 November 2013 03:57, Antoon Pardon mailto:antoon.par...@rece.vub.ac.be>> wrote:


 So I can now ask my questions in dutch and expect others to try and
 understand me instead of me asking them in english? Or can I use
 literal translations of dutch idioms even if I suspect that such
 a literal translation could be misunderstood and even be insulting?


1. No, because this is stated to be an English-speaking list/newsgroup.
It just doesn't specify what dialect of English.


Well so much for this group being an international group with only one
language allowed.

However that second sentence doesn't make much sense to me. Modern
languages contain a subset that is called the standard language.


Linguists would disagree.


Yes I accept that everyone deviates from this standard language and that
it isn't always easy to know what is and what is not within the standard
language and that we should allow each other some leeway.


Incorrect. No dialect "deviates" from a "standard" form of that language. 
Everyone speaks a dialect. The privilege given to any particular dialect has 
nothing to do with the form of the dialect itself and everything to do with the 
sociopolitical history of its speakers. None of that is relevant to speaking 
comprehensibly in an international environment.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Cracking hashes with Python

2013-11-27 Thread TheRandomPast .
Hi,

So apparently when I've been staring at code all day and tired my brain
doesn't tell my hands to type half of what I want it to. I apologise for my
last post.

This is my code;

import md5
import sys

characters=range(48,57)+range(65,90)+range(97,122)

def chklength(hash):
if len(hash) != 32:
print '[-] Improper length for md5 hash.'
sys.exit(1)

def checkPassword(password):
#print password
m = md5.new(password)
if (m.hexdigest() == hash):
print "match [" + password + "]"
sys.exit()

def recurse(width, position, baseString):
for char in characters:
if (position < width - 1):
recurse(width, position + 1, baseString + "%c" % char)
checkPassword(baseString + "%c" % char)
print "Target Hash [" + hash+ " string: "+ baseString

def brute_force():
maxChars = 32
for baseWidth in range(1, maxChars + 1):
print "checking passwords width [" + `baseWidth` + "]"
recurse(baseWidth, 0, "")

def dictionary():
for line in File.readlines():
checkPassword(line.strip('\n'))
hash =raw_input("Input MD5 hash:")
option=raw_input("Choose method:1=Brute Force; 0=Dictionary")
if(option==1):
chklength()
brute_force()
else:
if(option==0):
File=open("C:\dictionary.txt")
chklength()
dictionary()
else:
print "Wrong method!"

And dictionary is working, as is the brute force however the issue I have
having is with my chklength() as no matter how many characters I input it
skips the !=32 and goes straight to asking the user to chose either Brute
Force or Dictionary. I want an error to be shown if the hash is less than
or more than 32 characters but at present this chklength() doesn't work as
I thought it would.

Can anyone point out an obvious error that I am missing?


On Wed, Nov 27, 2013 at 2:58 AM, Chris Angelico  wrote:

> On Wed, Nov 27, 2013 at 1:55 PM, Tim Delaney
>  wrote:
> > Before I go look it up, I'm guessing that the etymology of "stumped" is
> > actually coming from the problem of a plough getting stuck on a stump
> (i.e.
> > can't progress any further). Not much of an issue anymore since the
> > invention of the stump-jump plough:
> > https://en.wikipedia.org/wiki/Stump-jump_plough
> >
>
> Australian inventiveness! We were too lazy to dig out the stumps
> before ploughing, so we came up with a solution.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Gene Heskett
On Wednesday 27 November 2013 07:44:18 rusi did opine:

> On Wednesday, November 27, 2013 7:41:54 AM UTC+5:30, Ned Batchelder 
wrote:
> > On 11/26/13 8:26 PM, Rick Johnson wrote:
> 
> 
> > And will you be here to explain to time-travelling Shakespeare why we
> > are all of us speaking English completely wrong (to his ears)?
> 
> And to my (Indian!!) ears when Tim says 'plank in the eye' where King
> James says 'beam' it does not cut it.

And around here, when something comes out correct, it is "better than a 
poke in the eye with a sharp stick" if you want to say it in the local 
idiom.  I used to have a B.I.L. who was always claiming "it was good enough 
for the girls I go with".

I think we, who have American English as our first and only language, 
should be giving allowances for the local idioms where the poster may be 
from, and really try to understand what the other posters mean when their 
unfamiliarity with what each of us might define as proper English might 
result in some ambiguity of our understanding of the question.  If after 
careful re-parsing of the statement, I still haven't understood it, then I 
am not allergic to asking for clarification IF I CAN CONTRIBUTE.  Otherwise 
I sit here and lurk, hoping to learn.

But in no case do I think we have a right to berate the original poster, 
who may not even speak the internet's default language, and may be looking 
up every word in a translation aid book, and doing it both ways to read 
what we might have written in response to what he/she posted.

Someone doing that, is in fact making 200x the effort to communicate 
compared to me.  We should be willing to deal with it, and do our best to 
return the favor.

We aren't doing that at all well when we start a week long thread over a 
miss-spelled word, which while humorous to some, are really nothing but 
contests to see who can come up with the next multi-syllable but totally 
meaningless word.  Its not humorous to those who are making the effort to 
communicate with those of us to whom some dialect of English is the first 
and only language.  Contributing to the confusion should not be the object 
here, but I think that is what we are doing by such action/reaction.

Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 

I wonder if I should put myself in ESCROW!!
A pen in the hand of this president is far more
dangerous than 200 million guns in the hands of
 law-abiding citizens.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cracking hashes with Python

2013-11-27 Thread Chris Angelico
On Wed, Nov 27, 2013 at 11:40 PM, TheRandomPast .
 wrote:
> And dictionary is working, as is the brute force however the issue I have
> having is with my chklength() as no matter how many characters I input it
> skips the !=32 and goes straight to asking the user to chose either Brute
> Force or Dictionary. I want an error to be shown if the hash is less than or
> more than 32 characters but at present this chklength() doesn't work as I
> thought it would.

You never pass it any argument. I don't know why it isn't throwing
TypeError at you.

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 6:27:52 PM UTC+5:30, Robert Kern wrote:
> On 2013-11-27 08:16, Antoon Pardon wrote:
> > Op 26-11-13 22:42, Tim Delaney schreef:
> >> On 27 November 2013 03:57, Antoon Pardon  wrote:
> >>  So I can now ask my questions in dutch and expect others to try and
> >>  understand me instead of me asking them in english? Or can I use
> >>  literal translations of dutch idioms even if I suspect that such
> >>  a literal translation could be misunderstood and even be insulting?
> >> 1. No, because this is stated to be an English-speaking list/newsgroup.
> >> It just doesn't specify what dialect of English.
> > Well so much for this group being an international group with only one
> > language allowed.
> > However that second sentence doesn't make much sense to me. Modern
> > languages contain a subset that is called the standard language.

> Linguists would disagree.

Linguists disagree a lot amongst themselves:

Early 20th century there was Fowler and his followers -- unabashedly
laying down the law on what is right and not.  Then there were his
opponents (French school I think, not sure what they were called --
poststructuralists maybe??) who said language was defined by usage and
not the other way.  Until someone (Fowlerite?) pointed out that those
anti-Fowlerites seemingly objectively described all the dialects but
they themselves stuck to pristine Queen's English.

So like in society, all dialects are equal and some are more equal!

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


Re: Wrapping around a list

2013-11-27 Thread Neil Cerutti
On Wed, Nov 27, 2013 at 5:46 AM,   wrote:
> I am working on a problem (Bioinformatics domain) where all
> possible combinations of input string needs to be printed as
> sublist
>
> For example:
> Input string : "LEQN"
> Output= "[L","E","Q","N"]
> ["LE","EQ","QN","NL"]["LEQ","EQN","QNE","NLE"]["LEQN"]

How about itertools.combinations?

import itertools

s = "LEQN"
for i in range(len(s)):
for comb in itertools.combinations(s, i+1):
print(comb)
Output:
('L',)
('E',)
('Q',)
('N',)
('L', 'E')
('L', 'Q')
('L', 'N')
('E', 'Q')
('E', 'N')
('Q', 'N')
('L', 'E', 'Q')
('L', 'E', 'N')
('L', 'Q', 'N')
('E', 'Q', 'N')
('L', 'E', 'Q', 'N')

For some reason I've got more 2-character combinations than you,
though.

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Rotwang

On 27/11/2013 08:31, Antoon Pardon wrote:

Op 27-11-13 09:19, Chris Angelico schreef:

[...]

Do you mean standard British English, standard American English,
standard Australian English, or some other?


Does that significantly matter or are you just looking for details
you can use to disagree? As far as I understand the overlap between
standard British English and standard American English is so large
that it doesn't really matter for those who had to learn the language.


The overlap is large, yes, but there are differences in vocabulary that 
are just as likely to cause confusion as the doubt/question distinction.


http://www.youtube.com/watch?v=LVzwJ6WWlHA&t=5m06s
--
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Robert Kern

On 2013-11-27 13:29, rusi wrote:

On Wednesday, November 27, 2013 6:27:52 PM UTC+5:30, Robert Kern wrote:

On 2013-11-27 08:16, Antoon Pardon wrote:

Op 26-11-13 22:42, Tim Delaney schreef:

On 27 November 2013 03:57, Antoon Pardon  wrote:
  So I can now ask my questions in dutch and expect others to try and
  understand me instead of me asking them in english? Or can I use
  literal translations of dutch idioms even if I suspect that such
  a literal translation could be misunderstood and even be insulting?
1. No, because this is stated to be an English-speaking list/newsgroup.
It just doesn't specify what dialect of English.

Well so much for this group being an international group with only one
language allowed.
However that second sentence doesn't make much sense to me. Modern
languages contain a subset that is called the standard language.



Linguists would disagree.


Linguists disagree a lot amongst themselves:

Early 20th century there was Fowler and his followers -- unabashedly
laying down the law on what is right and not.  Then there were his
opponents (French school I think, not sure what they were called --
poststructuralists maybe??) who said language was defined by usage and
not the other way.  Until someone (Fowlerite?) pointed out that those
anti-Fowlerites seemingly objectively described all the dialects but
they themselves stuck to pristine Queen's English.

So like in society, all dialects are equal and some are more equal!


Henry Fowler? To my knowledge, he was a dictionary-maker, not a linguist. To be 
fair, back then, the field of linguistics was not terribly well established, so 
he might have qualified for the title at the time. However, linguistics has 
learned a lot and moved on since then. You would be seriously hard-pressed to 
find a prescriptivist linguist these days.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Larry Martell
On Wed, Nov 27, 2013 at 5:31 AM, Paul Rudin  wrote:
> rusi  writes:
>
>> Propositionally: All languages are equal -- Turing complete
>
> As an aside, not all languages are Turing complete. For example Charity
> is a language with the property that programs are guaranteed to
> terminate.
>
> 

How about INTERCAL?

http://en.wikipedia.org/wiki/Intercal
-- 
https://mail.python.org/mailman/listinfo/python-list


Use of logging module to track TODOs

2013-11-27 Thread Jordi Riera
Hey list,

I always have issues with TODOs as they stay in the code and most of time 
forgot.
On that, I tried to find a way to track them and to be able to output them 
automatically. Automatically is a key as if devs have to do it manually it 
won't be done.

While I was checking the logging module and it features I thought about a 
little hack on it to use it to check files and output TODOs.
Logging check files and can have different channels for each level.
_Well, it needs to write another way the TODO as it will be now a new logging 
level and TODO would be wrote as logger.todo('Comment here')_

Here is my code:
___
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=W0511
# disabled the warning about TODOs
#
""" Adding a new level to the logging.
the new level will help to trace the TODOs in files.
"""
import logging
import os
import sys
import json
# setting the new level in logging module.
#
new_level = (logging.DEBUG + 1, 'TODO')
logging.addLevelName(*new_level)
logging.TODO = new_level[0]

class DictFormatter(logging.Formatter):
""" Formatter emitting data in the form of a dictionnary.

dict Pattern:
{
record.name: {
record.lineno: {
'message': record.getMessage(),
'created': self.formatTime(record, self.datefmt),
'funcName': record.funcName,
},
},
}
"""
def format(self, record):
""" Save the data in the json file and return the return
of the parent class.
"""
message = record.getMessage()

if record.exc_info:
# Cache the traceback text to avoid converting it multiple times
# (it's constant anyway)
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)

if record.exc_text:
try:
message = record.exc_text
except UnicodeError:
# Sometimes filenames have non-ASCII chars, which can lead
# to errors when s is Unicode and record.exc_text is str
# See issue 8924.
# We also use replace for when there are multiple
# encodings, e.g. UTF-8 for the filesystem and latin-1
# for a script. See issue 13232.
message = record.exc_text.decode(
sys.getfilesystemencoding(),
'replace'
)

return {
unicode(record.lineno): {
u'message': unicode(message),
u'created': unicode(self.formatTime(record, self.datefmt)),
u'funcName': unicode(record.funcName),
}
}


class SpecificJsonFileHandler(logging.FileHandler):
""" Specific FileHandler emitting a specific level of message.

:attribute level_message: logging level of message to consider. (int)
"""
def __init__(self, level_message, *args, **kwargs):
super(SpecificJsonFileHandler, self).__init__(*args, **kwargs)
self.__level_message = level_message

# To know if it is the first time the handler emit.
# It meant to know if we need to reset the data for this file in the 
json file.
#
self.__first_emit = True

def emit(self, record):
""" Only save in a json for the specific level of message. Skip all 
other messages."""
if record.levelno == self.__level_message:
with open(self.baseFilename, 'r') as json_file:
try:
data = json.load(json_file)
except ValueError:
data = {}

# Test if it is the fist time the instance will emit for the logger.
# At this point, the data from the logger is reset to avoid to keep
# cancelled/resolved todos.
#
if self.__first_emit:
data[record.name] = {}
self.__first_emit = False

data[record.name].update(self.format(record))

with open(self.baseFilename, 'w') as json_file:
json.dump(data, json_file, sort_keys=True, indent=4)


class TodoLogger(logging.getLoggerClass()):
""" Logger accepting the Todo parameter."""
__default_filename = 'log.log'

def __init__(self, *args, **kwargs):
super(TodoLogger, self).__init__(*args, **kwargs)
self.__handler = SpecificJsonFileHandler(
logging.TODO,
kwargs.get('todo_filename', self.__default_filename),
)
self.__handler.setLevel(logging.TODO)
self.__handler.setFormatter(
DictFormatter()
)

self.addHandler(self.__handler)

def todo(self, message, *args, **kwargs):
""" Log 'msg % args' with severity 'TODO'.

To pass exc

Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 7:39:37 PM UTC+5:30, Larry wrote:
> On Wed, Nov 27, 2013 at 5:31 AM, Paul Rudin  wrote:
> > rusi  writes:
> >> Propositionally: All languages are equal -- Turing complete
> > As an aside, not all languages are Turing complete. For example Charity
> > is a language with the property that programs are guaranteed to
> > terminate.

> How about INTERCAL?

> http://en.wikipedia.org/wiki/Intercal

Oh its Turing complete alright:
http://en.wikipedia.org/wiki/Intercal#Details

It would be more apt to say that programmers use dozens of 'languages'
(in the sense of notation) that are very far up/down from Turing
equivalent. eg

- regular expressions and parsing tools like yacc are less than Turing
  equivalent
- specification langauages like Z/UML are more powerful than Turing
  machines in that one can specify unimplementable programs

So when I say 'language' strictly I should say 'programming language'

If (something like) Charity succeeds *as a programming language* then
it will be a significant change in how we view programming.  As a thought 
experiment that is interesting but I would be skeptical…
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Steven D'Aprano
On Wed, 27 Nov 2013 09:09:37 -0500, Larry Martell wrote:

> On Wed, Nov 27, 2013 at 5:31 AM, Paul Rudin 
> wrote:
>> rusi  writes:
>>
>>> Propositionally: All languages are equal -- Turing complete
>>
>> As an aside, not all languages are Turing complete. For example Charity
>> is a language with the property that programs are guaranteed to
>> terminate.
>>
>> 
> 
> How about INTERCAL?
> 
> http://en.wikipedia.org/wiki/Intercal


Intercal is Turing complete.

*Not* being Turing complete is normally a bad thing, at least for a full-
blown programming language. On the other hand, a less powerful non-Turing 
complete language would probably be great for things like user-defined 
macros, plugins, and similar, where the users are not entirely trusted.



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


Re: parsing nested unbounded XML fields with ElementTree

2013-11-27 Thread Larry Martell
On Tue, Nov 26, 2013 at 8:20 AM, Stefan Behnel  wrote:
> Larry Martell, 26.11.2013 13:23:
>> On Tue, Nov 26, 2013 at 2:38 AM, Stefan Behnel wrote:
>>> larry.martell...@gmail.com, 25.11.2013 23:22:
 I have an XML file that has an element called "Node". These can be nested 
 to any depth and the depth of the nesting is not known to me. I need to 
 parse the file and preserve the nesting. For exmaple, if the XML file had:

 

   
 
   

 When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I 
 don't know how deep this can be. This is the code I have so far:

 nodes = []

 def parseChild(c):
 if c.tag == 'Node':
 if 'Name' in c.attrib:
 nodes.append(c.attrib['Name'])
 for c1 in c:
 parseChild(c1)
 else:
 for node in nodes:
 print node,
 print c.tag

 for parent in tree.getiterator():
 for child in parent:
 for x in child:
 parseChild(x)
>>>
>>> This seems hugely redundant. tree.getiterator() already returns a recursive
>>> iterable, and then, for each nodes in your document, you are running
>>> recursively over its entire subtree. Meaning that you'll visit each node as
>>> many times as its depth in the tree.
>>>
>>>
 My problem is that I don't know when I'm done with a node and I should
 remove a level of nesting. I would think this is a fairly common
 situation, but I could not find any examples of parsing a file like
 this. Perhaps I'm going about it completely wrong.
>>>
>>> Your recursive traversal function tells you when you're done. If you drop
>>> the getiterator() bit, reaching the end of parseChild() means that you're
>>> done with the element and start backing up. So you can simply pass down a
>>> list of element names that you append() at the beginning of the function
>>> and pop() at the end, i.e. a stack. That list will then always give you the
>>> current path from the root node.
>>
>> Thanks for the reply. How can I remove getiterator()? Then I won't be
>> traversing the nodes of the tree. I can't iterate over tree. I am also
>> unclear on where to do the pop(). I tried putting it just after the
>> recursive call to parseChild() and I tried putting as the very last
>> statement in parseChild() - neither one gave the desired result. Can
>> you show me in code what you mean?
>
> untested:
>
>   nodes = []
>
>   def process_subtree(c, path):
>   name = c.get('Name') if c.tag == 'Node' else None
>   if name:
>   path.append(name)
>   nodes.append('/'.join(path))
>
>   for c1 in c:
>   process_subtree(c1, path)
>
>   if name:
>   path.pop()
>
>   process_subtree(tree.getroot(), [])

Thanks! This was extremely helpful and I've use these concepts to
write script that successfully parses my file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Use of logging module to track TODOs

2013-11-27 Thread Jean-Michel Pichavant


- Original Message -
> Hey list,

Greetings !


> How do you do with your TODOs?
> 
> Regards,
> Jordi

TODOs always share the same fate : they get forgotten and wander the code until 
the project dies. Unless you have the required mental resilience to stop the 
urgent work to fix your TODOs once a week, this is gonna happen.

Here's how I'm dealing with TODOs:
1/ I instantly remove them.
2/ I force myself to fix them right now, when possible, If not, I'll have to 
live without.

At least, it works for me.

JM

Note : fixing the TODO right now is efficient as you don't need to switch your 
working context a few weeks later.


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Use of logging module to track TODOs

2013-11-27 Thread Mark Lawrence

On 27/11/2013 14:52, Jean-Michel Pichavant wrote:



- Original Message -

Hey list,


Greetings !


How do you do with your TODOs?

Regards,
Jordi


TODOs always share the same fate : they get forgotten and wander the code until 
the project dies. Unless you have the required mental resilience to stop the 
urgent work to fix your TODOs once a week, this is gonna happen.

Here's how I'm dealing with TODOs:
1/ I instantly remove them.
2/ I force myself to fix them right now, when possible, If not, I'll have to 
live without.

At least, it works for me.

JM

Note : fixing the TODO right now is efficient as you don't need to switch your 
working context a few weeks later.



This, from the Zen of Python, seems applicable in this context.

Now is better than never.
Although never is often better than *right* now.

--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: tkinter bug on mac maverick python 3.3.3

2013-11-27 Thread Terry Reedy

On 11/27/2013 6:48 AM, Ned Batchelder wrote:

On 11/27/13 6:32 AM, Dan Wissme wrote:

Hi !
Am I the only one to get a bug in GUIs using tkinter on my Mac under
maverick and Python 3.3.3 ?
When will they get rid of Tcl/Tk which causes recurrent problems at
almost each new Python version !
Please, for the rest of us...

-dan


It sounds like you're really frustrated, I'm sorry.  I don't know if you
want help with your actual bug, if so, provide details.  If it's any
consolation, Guido himself is also frustrated:
https://twitter.com/gvanrossum/status/404768253282484224


As noted in the Twitter thread, the problem is with Apple shenanigans. 
Tk and hence Idle have steadily worked on Windows for at least a decade.



--
Terry Jan Reedy

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


Re: UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

2013-11-27 Thread Terry Reedy

On 11/27/2013 7:37 AM, Tim Golden wrote:

On 27/11/2013 08:31, Hoàng Tuấn Việt wrote:

I cannot see your reply in python-list until I search this question
on Internet again.


(cc-ing the OP because of the nature of the problem)

Viet,

That's because you're not subscribed to the list. The way it works is
that you subscribe to the list and all your posts go to everyone else
who's subscribed and you see all their posts. At present, you're
operating as a "guest" which means that one of us has to approve every
post you make.

It looks as though Fabio's solved your current problem, but please try
subscribing if you want to post again:

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


Or post through news.gmane.org.

--
Terry Jan Reedy


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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Ned Batchelder

On 11/27/13 8:18 AM, Gene Heskett wrote:

On Wednesday 27 November 2013 07:44:18 rusi did opine:


On Wednesday, November 27, 2013 7:41:54 AM UTC+5:30, Ned Batchelder

wrote:

On 11/26/13 8:26 PM, Rick Johnson wrote:




And will you be here to explain to time-travelling Shakespeare why we
are all of us speaking English completely wrong (to his ears)?


And to my (Indian!!) ears when Tim says 'plank in the eye' where King
James says 'beam' it does not cut it.


And around here, when something comes out correct, it is "better than a
poke in the eye with a sharp stick" if you want to say it in the local
idiom.  I used to have a B.I.L. who was always claiming "it was good enough
for the girls I go with".

I think we, who have American English as our first and only language,
should be giving allowances for the local idioms where the poster may be
from, and really try to understand what the other posters mean when their
unfamiliarity with what each of us might define as proper English might
result in some ambiguity of our understanding of the question.  If after
careful re-parsing of the statement, I still haven't understood it, then I
am not allergic to asking for clarification IF I CAN CONTRIBUTE.  Otherwise
I sit here and lurk, hoping to learn.


+1


But in no case do I think we have a right to berate the original poster,
who may not even speak the internet's default language, and may be looking
up every word in a translation aid book, and doing it both ways to read
what we might have written in response to what he/she posted.

Someone doing that, is in fact making 200x the effort to communicate
compared to me.  We should be willing to deal with it, and do our best to
return the favor.


+1


We aren't doing that at all well when we start a week long thread over a
miss-spelled word, which while humorous to some, are really nothing but
contests to see who can come up with the next multi-syllable but totally
meaningless word.  Its not humorous to those who are making the effort to
communicate with those of us to whom some dialect of English is the first
and only language.  Contributing to the confusion should not be the object
here, but I think that is what we are doing by such action/reaction.


Thanks, I think this sums up the situation very well.

--Ned.



Cheers, Gene




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


Re: Cracking hashes with Python

2013-11-27 Thread Laszlo Nagy

On 2013-11-26 00:58, Marc wrote:

Hashes, by definition, are not reversible mathematically.  The only way to
figure out what they represent is to take plaintext that might be the
plaintext based on anything you might know about the original plaintext
(which is often nothing) and hash it; then see if the hash matches the one
you have.  If it does, you have figured out the plaintext; if it doesn't try
again.  For a tool that does this, look at Rainbow tables.
There are also complete hash databases on the internet. They usually 
reverse-map hash values to most common values found in dictionaries.


Here is an example:

>>> import hashlib
>>> h = hashlib.md5("test")
>>> h.hexdigest()
'098f6bcd4621d373cade4e832627b4f6'
>>>


Then you go here:

http://www.md5decrypter.co.uk/

There are many other databases like this, search for them with "md5 
search" or "sha1 database" etc.

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


Re: Wrapping around a list

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 4:16:50 PM UTC+5:30, Amjad Syed wrote:
> Hello,
> 
> I am working on a problem (Bioinformatics domain) where all possible 
> combinations of input string needs to be printed as sublist

If we take the standard combinations (Pascal triangle) result
nCr + nCr-1 = n+1Cr
and make it into a recursive python function we get:

def c(n,r):
return (1 if r == 0 else
   0 if n == 0 else
   c(n-1,r) + c(n-1,r-1))

(yeah the base cases are tricky)

Now instead of enumerating the *number* of combinations if we want the 
combinations *themselves* we can do almost isomorphically:
[Writing d analogous to c]

def d(l,r):
if r == 0: return [[]]
if not l: return []   
x  = l[0]
xs = l[1:]
return d(xs,r) + [[x]+c for c in d(xs,(r-1))]

Note the difference in types:

>>> c(4,2)
6
>>> d("LEQN", 2)
[['Q', 'N'], ['E', 'N'], ['E', 'Q'], ['L', 'N'], ['L', 'Q'], ['L', 'E']]
>>> 

Now we can generator-ize it like so:

def e(l,r):
if r == 0:
yield []
elif l:
x  = l[0]
xs = l[1:]
for y in chain(e(xs,r),([x]+c for c in e(xs,(r-1:
yield y
# python 3: yield from chain(e(xs,r),([x]+c for c in e(xs,(r-1


called as
>>> list(e("LEQN", 2))
[['Q', 'N'], ['E', 'N'], ['E', 'Q'], ['L', 'N'], ['L', 'Q'], ['L', 'E']]
>>> 

BTW: This is neater in Haskell than in Python.

c n 0 = 1
c 0 r = 0
c n r = c (n-1) r + c (n-1) (r-1) 

   
d l  0 = [[]]
d [] r = [] 
d (x:xs) r = d xs r ++ [x:c | c <- d xs (r-1)]

In particular the 'd' has list elegance of the python 'd' and generator 
efficiency of python 'e'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Tim Chase
On 2013-11-27 01:32, rusi wrote:
> > And will you be here to explain to time-travelling Shakespeare
> > why we are all of us speaking English completely wrong (to his
> > ears)?  
> 
> And to my (Indian!!) ears when Tim says 'plank in the eye' where
> King James says 'beam' it does not cut it.

Well, would you have preferred it in the "correct" form, in
which I could have said "δοκος εν τω οφθαλμω σου"? ;-)

-tkc



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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread rusi
On Wednesday, November 27, 2013 9:55:12 PM UTC+5:30, Tim Chase wrote:
> On 2013-11-27 01:32, rusi wrote:
> > > And will you be here to explain to time-travelling Shakespeare
> > > why we are all of us speaking English completely wrong (to his
> > > ears)?  
> > And to my (Indian!!) ears when Tim says 'plank in the eye' where
> > King James says 'beam' it does not cut it.

> Well, would you have preferred it in the "correct" form, in
> which I could have said "δοκος εν τω οφθαλμω σου"? ;-)

Heh!

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


Re: Parallel Python x.y.A and x.y.B installations on a single Windows machine

2013-11-27 Thread Jurko Gospodnetić

  Hi.

On 25.11.2013. 14:42, Jurko Gospodnetić wrote:

   So far all tests seem to indicate that things work out fine if we
install to some dummy target folder, copy the target folder to some
version specific location & uninstall. That leaves us with a working
Python folder sans the start menu and registry items, both of which we
do not need for this. Everything I've played around with so far seems to
use the correct Python data depending on the interpreter executable
invoked, whether or not there is a regular Windows installation
somewhere on the same machine.

   We can use the script suggested by Ned Batchelder to temporarily
change the 'current installation' if needed for some external installer
package to correctly recognize where to install its content.

   I'm still playing around with this, and will let you know how it goes.


  Just wanted to let you know that the usage I described above seems to 
work in all the cases I tried out.


  I added some batch scripts for running a specific Python interpreter 
as a convenience and everything works 'naturally' in our development 
environment.


  Packages can be easily installed to a specific targeted environment 
using for example:

  py243 -m easy_install pip
  py332 -m pip install pytest
[not mentioning tweaks needed for specific ancient Python versions]

  Thank you all for all the suggestions.

  Best regards,
Jurko Gospodnetić


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


Re: Cracking hashes with Python

2013-11-27 Thread MRAB

On 27/11/2013 12:40, TheRandomPast . wrote:

Hi,

So apparently when I've been staring at code all day and tired my brain
doesn't tell my hands to type half of what I want it to. I apologise for
my last post.

This is my code;

import md5
import sys

characters=range(48,57)+range(65,90)+range(97,122)

def chklength(hash):
 if len(hash) != 32:
 print '[-] Improper length for md5 hash.'
 sys.exit(1)

def checkPassword(password):
 #print password
 m = md5.new(password)
 if (m.hexdigest() == hash):
 print "match [" + password + "]"
 sys.exit()
def recurse(width, position, baseString):
 for char in characters:
 if (position < width - 1):
 recurse(width, position + 1, baseString + "%c" % char)
 checkPassword(baseString + "%c" % char)
 print "Target Hash [" + hash+ " string: "+ baseString

def brute_force():
 maxChars = 32
 for baseWidth in range(1, maxChars + 1):
 print "checking passwords width [" + `baseWidth` + "]"
 recurse(baseWidth, 0, "")

def dictionary():
 for line in File.readlines():
 checkPassword(line.strip('\n'))
hash =raw_input("Input MD5 hash:")
option=raw_input("Choose method:1=Brute Force; 0=Dictionary")
if(option==1):
 chklength()
 brute_force()
else:
 if(option==0):
 File=open("C:\dictionary.txt")
 chklength()
 dictionary()
 else:
 print "Wrong method!"

And dictionary is working, as is the brute force however the issue I
have having is with my chklength() as no matter how many characters I
input it skips the !=32 and goes straight to asking the user to chose
either Brute Force or Dictionary. I want an error to be shown if the
hash is less than or more than 32 characters but at present this
chklength() doesn't work as I thought it would.

Can anyone point out an obvious error that I am missing?


[snip]

You're asking for the hash, then you're asking for the method. You're
not checking the length of the hash between the two.

BTW, 'raw_input' returns a string and a string != a number, e.g. "1" !=
1.

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


Re: Wrapping around a list

2013-11-27 Thread Peter Pearson
On Wed, 27 Nov 2013 08:33:43 -0500, Neil Cerutti  wrote:
> On Wed, Nov 27, 2013 at 5:46 AM,   wrote:
>> I am working on a problem (Bioinformatics domain) where all
>> possible combinations of input string needs to be printed as
>> sublist
>>
>> For example:
>> Input string : "LEQN"
>> Output= "[L","E","Q","N"]
>> ["LE","EQ","QN","NL"]["LEQ","EQN","QNE","NLE"]["LEQN"]
>
> How about itertools.combinations?
>
> import itertools
>
> s = "LEQN"
> for i in range(len(s)):
> for comb in itertools.combinations(s, i+1):
> print(comb)
> Output:
> ('L',)
> ('E',)
> ('Q',)
> ('N',)
> ('L', 'E')
> ('L', 'Q')
> ('L', 'N')
> ('E', 'Q')
> ('E', 'N')
> ('Q', 'N')
> ('L', 'E', 'Q')
> ('L', 'E', 'N')
> ('L', 'Q', 'N')
> ('E', 'Q', 'N')
> ('L', 'E', 'Q', 'N')
>
> For some reason I've got more 2-character combinations than you,
> though.

The original poster's combinations comprise letters that are *contiguous*,
in a circular sense.  Yours include non-contiguous sets, like LQ.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python for everyday tasks

2013-11-27 Thread Pavel Volkov
On Saturday 23 November 2013 02:01:26 Steven D'Aprano wrote:
> * Python is not Java, and Java is not Python either:
> 
> http://dirtsimple.org/2004/12/python-is-not-java.html
> http://dirtsimple.org/2004/12/java-is-not-python-either.html

Thanks for all those references.
There's this statement in the first article:

"Got a switch statement? The Python translation is a hash table, not a bunch 
of if-then statments. Got a bunch of if-then's that wouldn't be a switch 
statement in Java because strings are involved? It's still a hash table. "

I can't figure out how would you translate a switch statement into hash table 
in general case.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Wrapping around a list

2013-11-27 Thread Amjad Syed
On Wednesday, November 27, 2013 2:14:18 PM UTC+3, Chris Angelico wrote:
> On Wed, Nov 27, 2013 at 10:07 PM, Amjad Syed  wrote:
> 
> > Thanks Chris for the reply. But i would like sliding function to be 
> > scalable, as input string can be of 100 letters.
> 
> 
> 
> A hundred isn't much to work with, and your code will be fairly
> 
> simple. Give it a try with small strings, see how it goes; then try it
> 
> on your full-size - I doubt there'll be a problem.
> 
> 
> 
> Now, if you were talking about a hundred million letters, then maybe
> 
> there'd be a problem. But even there, I'd start with the simple and
> 
> clean approach, and only optimize for performance when it becomes
> 
> obviously necessary (like when my MUD client was able to saturate one
> 
> core of my CPU, just by me typing a lot of commands very rapidly -
> 
> that needed fixing!).
> 
> 
> 
> ChrisA

Thanks Chris, the duplication did solve the problem. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python for everyday tasks

2013-11-27 Thread Michael Torrie
On 11/27/2013 11:05 AM, Pavel Volkov wrote:
> Thanks for all those references.
> There's this statement in the first article:
> 
> "Got a switch statement? The Python translation is a hash table, not a bunch 
> of if-then statments. Got a bunch of if-then's that wouldn't be a switch 
> statement in Java because strings are involved? It's still a hash table. "
> 
> I can't figure out how would you translate a switch statement into hash table 
> in general case.

The general case is an if/elif ladder.  But consider:

def func1(): pass
def func2(): pass
def func3(): pass

dispatch = { 0: func1,
 1: func2,
 2: func3,
   }

# do some calc
result = somecalc()
try:
  dispatch[result]()
except KeyError:
  # invalid result

That's what the article is talking about.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python for everyday tasks

2013-11-27 Thread Jean-Michel Pichavant
- Original Message -
> On Saturday 23 November 2013 02:01:26 Steven D'Aprano wrote:
> > * Python is not Java, and Java is not Python either:
> > 
> > http://dirtsimple.org/2004/12/python-is-not-java.html
> > http://dirtsimple.org/2004/12/java-is-not-python-either.html
> 
> Thanks for all those references.
> There's this statement in the first article:
> 
> "Got a switch statement? The Python translation is a hash table, not
> a bunch
> of if-then statments. Got a bunch of if-then's that wouldn't be a
> switch
> statement in Java because strings are involved? It's still a hash
> table. "
> 
> I can't figure out how would you translate a switch statement into
> hash table
> in general case.
> --
> https://mail.python.org/mailman/listinfo/python-list
> 

It actually refers to python dictionaries, they use hash-able object as keys:

switch (foo):
  1: do_something()
  2: do_someotherthing()

can be written in python that way:

{
  1: do_something,
  2: do_someotherthing,
}[foo]()

There are some limitation though, without going into details.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter bug on mac maverick python 3.3.3

2013-11-27 Thread Travis Griggs

On Nov 27, 2013, at 3:32 AM, Dan Wissme  wrote:

> Hi !
> Am I the only one to get a bug in GUIs using tkinter on my Mac under maverick 
> and Python 3.3.3 ?
> When will they get rid of Tcl/Tk which causes recurrent problems at almost 
> each new Python version !
> Please, for the rest of us...

I’m curious, if they get rid of Tcl/Tk as you wished, what do you propose 
replacing it with?

It’s not like there are other “light weight cross platform ui frameworks” that 
are obvious replacements. Most of the others are far heavier. And all have 
issues with their purported cross platformness.

Or are you proposing that Tcl/Tk be moved out of the python core distro, and 
instead delivered as a separate package? If *this* is your proposal, I 
wholeheartedly agree. Just the other day, I was working on putting python3 on a 
beaglebone black (similar to a raspberry pi). It built OK, but I had to ignore 
lots of warnings about Tcl/Tk not working, which of course was a “duh”.

I was surprised, that among some, there’s a sentiment that python core MUST 
include it. Which was interesting. One of the core principles of this language 
is all about modules and modularity. Why can’t the Tcl/Tk module be the same as 
numpy and scipy and many of the other widely 
installed-after-the-fact-as-appropriate packages?
-- 
https://mail.python.org/mailman/listinfo/python-list


'_[1]' in .co_names using builtin compile() in Python 2.6

2013-11-27 Thread magnus.ly...@gmail.com
When I run e.g. compile('sin(5) * cos(6)', '', 'eval').co_names, I get 
('sin', 'cos'), which is just what I expected.

But when I have a list comprehension in the expression, I get a little surprise:
>>> compile('[x*x for x in y]',  '', 'eval').co_names
('_[1]', 'y', 'x')
>>>

This happens in Python 2.6.6 on Red Hat Linux, but not when I run Python 2.7.3 
in Windows. Unfortunately I'm stuck with 2.6.

* Are there more surprises similar to this one that I can expect from 
compile(...).co_names? Is this "behaviour" documented somewhere?

* Is there perhaps a better way to achieve what I'm trying to do?

What I'm really after, is to check that python expressions embedded in text 
files are:
- well behaved (no syntax errors etc)
- don't accidentally access anything it shouldn't
- I serve them with the values they need on execution

So, in the case of "a.b + x" I'm really just interested in a and x, not b. So 
the (almost) whole story is that I do:

# Find names not starting with ".", i.e a & b in "a.c + b"
abbr_expr = re.sub(r"\.\w+", "", expr)
names = compile(abbr_expr, '', 'eval').co_names
# Python 2.6 returns '_[1]' in co_names for list comprehension. Bug?
names = [name for name in names if re.match(r'\w+$', name)]

for name in names:
if name not in allowed_names:
raise NameError('Name: %s not permitted in expression: %s' % (name, 
expr))

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


Re: '_[1]' in .co_names using builtin compile() in Python 2.6

2013-11-27 Thread Ned Batchelder

On 11/27/13 2:40 PM, magnus.ly...@gmail.com wrote:

When I run e.g. compile('sin(5) * cos(6)', '', 'eval').co_names, I get 
('sin', 'cos'), which is just what I expected.

But when I have a list comprehension in the expression, I get a little surprise:

compile('[x*x for x in y]',  '', 'eval').co_names

('_[1]', 'y', 'x')




This happens in Python 2.6.6 on Red Hat Linux, but not when I run Python 2.7.3 
in Windows. Unfortunately I'm stuck with 2.6.

* Are there more surprises similar to this one that I can expect from 
compile(...).co_names? Is this "behaviour" documented somewhere?



That name is the name of the list being built by the comprehension, 
which I found out by disassembling the code object to see the bytecodes:


>>> co = compile("[x*x for x in y]", "", "eval")
>>> co.co_names
('_[1]', 'y', 'x')
>>> import dis
>>> dis.dis(co)
  1   0 BUILD_LIST   0
  3 DUP_TOP
  4 STORE_NAME   0 (_[1])
  7 LOAD_NAME1 (y)
 10 GET_ITER
>>   11 FOR_ITER17 (to 31)
 14 STORE_NAME   2 (x)
 17 LOAD_NAME0 (_[1])
 20 LOAD_NAME2 (x)
 23 LOAD_NAME2 (x)
 26 BINARY_MULTIPLY
 27 LIST_APPEND
 28 JUMP_ABSOLUTE   11
>>   31 DELETE_NAME  0 (_[1])
 34 RETURN_VALUE

The same list comprehension in 2.7 uses an unnamed list on the stack:

  1   0 BUILD_LIST   0
  3 LOAD_NAME0 (y)
  6 GET_ITER
>>7 FOR_ITER16 (to 26)
 10 STORE_NAME   1 (x)
 13 LOAD_NAME1 (x)
 16 LOAD_NAME1 (x)
 19 BINARY_MULTIPLY
 20 LIST_APPEND  2
 23 JUMP_ABSOLUTE7
>>   26 RETURN_VALUE

I don't know whether such facts are documented.  They are deep 
implementation details, and change from version to version, as you've seen.



* Is there perhaps a better way to achieve what I'm trying to do?

What I'm really after, is to check that python expressions embedded in text 
files are:
- well behaved (no syntax errors etc)
- don't accidentally access anything it shouldn't
- I serve them with the values they need on execution


I hope you aren't trying to prevent malice this way: you cannot examine 
a piece of Python code to prove that it's safe to execute.  For an 
extreme example, see: Eval Really Is Dangerous: 
http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html


In your environment it looks like you have a whitelist of identifiers, 
so you're probably ok.




So, in the case of "a.b + x" I'm really just interested in a and x, not b. So 
the (almost) whole story is that I do:

 # Find names not starting with ".", i.e a & b in "a.c + b"
 abbr_expr = re.sub(r"\.\w+", "", expr)
 names = compile(abbr_expr, '', 'eval').co_names
 # Python 2.6 returns '_[1]' in co_names for list comprehension. Bug?
 names = [name for name in names if re.match(r'\w+$', name)]

 for name in names:
 if name not in allowed_names:
 raise NameError('Name: %s not permitted in expression: %s' % 
(name, expr))



I don't know of a better way to determine the real names in the 
expression.  I doubt Python will insert a valid name into the namespace, 
since it doesn't want to step on real user names.  The simplest way to 
do that is to autogenerate invalid names, like "_[1]" (I wonder why it 
isn't "_[0]"?)


--Ned.

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


Re: tkinter bug on mac maverick python 3.3.3

2013-11-27 Thread Ned Deily
In article <5295d83d$0$3458$426a3...@news.free.fr>,
 Dan Wissme  wrote:
> Am I the only one to get a bug in GUIs using tkinter on my Mac under 
> maverick and Python 3.3.3 ?
> When will they get rid of Tcl/Tk which causes recurrent problems at 
> almost each new Python version !
> Please, for the rest of us...

If the problem you are referring to is that Tk widgets are not being redrawn 
properly until you move the cursor over them or type in them, follow the 
instructions here and install the very latest version of ActiveTcl 8.5 for OS 
X (labeled 8.5.15.0 on the web site but the download file is 8.5.15.1).

http://www.python.org/download/mac/tcltk/

-- 
 Ned Deily,
 n...@acm.org

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


Re: '_[1]' in .co_names using builtin compile() in Python 2.6

2013-11-27 Thread Ian Kelly
On Nov 27, 2013 2:11 PM, "Ned Batchelder"  wrote:
>
> On 11/27/13 2:40 PM, magnus.ly...@gmail.com wrote:
>>
>> So, in the case of "a.b + x" I'm really just interested in a and x, not
b. So the (almost) whole story is that I do:
>>
>>  # Find names not starting with ".", i.e a & b in "a.c + b"
>>  abbr_expr = re.sub(r"\.\w+", "", expr)
>>  names = compile(abbr_expr, '', 'eval').co_names
>>  # Python 2.6 returns '_[1]' in co_names for list comprehension. Bug?
>>  names = [name for name in names if re.match(r'\w+$', name)]
>>
>>  for name in names:
>>  if name not in allowed_names:
>>  raise NameError('Name: %s not permitted in expression: %s'
% (name, expr))
>>
>
> I don't know of a better way to determine the real names in the
expression.  I doubt Python will insert a valid name into the namespace,
since it doesn't want to step on real user names.  The simplest way to do
that is to autogenerate invalid names, like "_[1]" (I wonder why it isn't
"_[0]"?)

One possible alternative is to use the ast module to examine the parse tree
of the expression instead of the generated code object. Hard to say whether
that would be "better".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Static Website Generator

2013-11-27 Thread Silvio Siefke
Hello,

On Sat, 16 Nov 2013 11:43:01 -0500 Veritatem Ignotam
 wrote:

> I'll second Nikola (full disclosure: not affiliated)

Thank you for advice. Nikola is good, i good convert my blog in it and
my theme i have convert. Only two things i miss:

I find no way the blog article in the index file to limit. At moment
my index File give last 5 Blog Article and the last 5 tutorials out. In
Nikola i find not the way to limited. 

Secound what not really understand. There give Post Articles, i can use 
for Blog, and there are Stories. The Stories can use for Tutorials. The 
problem is the article from the stories folder no where be linked. 
Neither the index not found in the folder a index file.

Is here someone familar with the system and can help?

Thank you & Greetings
Silvio
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Static Website Generator

2013-11-27 Thread Ben Finney
Silvio Siefke  writes:

> Thank you for advice. Nikola is good, i good convert my blog in it and
> my theme i have convert. Only two things i miss:

[…]

> Is here someone familar with the system and can help?

It would be better to ask for Nikola help on the Nikola-specific
discussion forums http://getnikola.com/contact.html>.

-- 
 \   “I do not believe in forgiveness as it is preached by the |
  `\church. We do not need the forgiveness of God, but of each |
_o__)other and of ourselves.” —Robert G. Ingersoll |
Ben Finney

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


Re: '_[1]' in .co_names using builtin compile() in Python 2.6

2013-11-27 Thread Chris Kaynor
On Wed, Nov 27, 2013 at 12:09 PM, Ned Batchelder wrote:

> * Is there perhaps a better way to achieve what I'm trying to do?
>>
>> What I'm really after, is to check that python expressions embedded in
>> text files are:
>> - well behaved (no syntax errors etc)
>> - don't accidentally access anything it shouldn't
>> - I serve them with the values they need on execution
>>
>
> I hope you aren't trying to prevent malice this way: you cannot examine a
> piece of Python code to prove that it's safe to execute.  For an extreme
> example, see: Eval Really Is Dangerous: http://nedbatchelder.com/blog/
> 201206/eval_really_is_dangerous.html
>
> In your environment it looks like you have a whitelist of identifiers, so
> you're probably ok.


I just tested the crash example from that link in Python 2.7.5 win64 and
the co_names from the compiled code is empty. Therefore, a simple whitelist
would not catch that problematic code (and likely any other global access
done correctly). Even a simple test of making sure that at least one (or
any number of) valid identifier exists would be insufficent, as you can
merely tack on a ",a" to add "a" to the co_names, and thus for any other
variable.

Basically, even with a pure whitelist, there is likely no possible way to
make eval/exec safe, unless you also eliminate the ability to make literals.

Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cracking hashes with Python

2013-11-27 Thread Denis McMahon
On Wed, 27 Nov 2013 12:40:41 +, TheRandomPast . wrote:

> And dictionary is working, as is the brute force however the issue I
> have having is with my chklength() as no matter how many characters I
> input it skips the !=32 and goes straight to asking the user to chose
> either Brute Force or Dictionary. I want an error to be shown if the
> hash is less than or more than 32 characters but at present this
> chklength() doesn't work as I thought it would.
> 
> Can anyone point out an obvious error that I am missing?

Yes - you don't need to type in the hashes by hand at all.

You have code to read the hashes from a web page as strings.

Read the hashes from the web into a list of hashes.
Read the dictionary file into a list of words.
for each word in the dictionary compare the hashed word with the list of 
hashes, and if you get a match, output the word and the hash.

If you have code to get the hash strings from the web url, it is nuts to 
output them to the screen and then type them back in to the cracking 
program when you can just add the code to get them from the web to the 
cracking program.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: '_[1]' in .co_names using builtin compile() in Python 2.6

2013-11-27 Thread Ned Batchelder

On 11/27/13 3:44 PM, Chris Kaynor wrote:

On Wed, Nov 27, 2013 at 12:09 PM, Ned Batchelder mailto:n...@nedbatchelder.com>> wrote:

* Is there perhaps a better way to achieve what I'm trying to do?

What I'm really after, is to check that python expressions
embedded in text files are:
- well behaved (no syntax errors etc)
- don't accidentally access anything it shouldn't
- I serve them with the values they need on execution


I hope you aren't trying to prevent malice this way: you cannot
examine a piece of Python code to prove that it's safe to execute.
  For an extreme example, see: Eval Really Is Dangerous:
http://nedbatchelder.com/blog/__201206/eval_really_is___dangerous.html


In your environment it looks like you have a whitelist of
identifiers, so you're probably ok.


I just tested the crash example from that link in Python 2.7.5 win64 and
the co_names from the compiled code is empty. Therefore, a simple
whitelist would not catch that problematic code (and likely any other
global access done correctly). Even a simple test of making sure that at
least one (or any number of) valid identifier exists would be
insufficent, as you can merely tack on a ",a" to add "a" to the
co_names, and thus for any other variable.


Ah, right you are! I neglected to go back and examine the dangerous 
code.  So eval really is dangerous!


--Ned.



Basically, even with a pure whitelist, there is likely no possible way
to make eval/exec safe, unless you also eliminate the ability to make
literals.

Chris





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


Access database - GUI - Python - I need architectural advice

2013-11-27 Thread jm.almeras

Hello !

I wish to develop a database application with a lot of specific 
functionnalities dealing with sound files.


I have developped an Access prototype and run into a first problem : it 
is not so easy to find code working with VBA to extract the duration of 
a sound file. I have found many code samples, none works perfectly with 
all sound files (variable rates, ...).


A second problem I have is that I recently started programming with 
Python, and gosh, the idea of coding with VBA after tasting Python is 
like going back to black & white television after tasting color and 3D !


Access, and more generally VB, is excellent for building the GUI (forms, 
widgets etc.). Python is a great for coding, and it comes with high 
quality libraries... Does anyone have any suggestions as to how I can 
build my database app with nice Access-like GUI, and programming with 
Python ?


One idea that has come to my mind is to store python modules in the 
Access database and write VBA code to execute these on the fly... Is 
this crazy ?


Thank you for any ideas you might have...

Jean-Marie

PS : just to make my need clear and precise, here is a list of 
functionnalities that I will have in my application : register sound 
files with all their characteristics (duration, ...), play (including 
quick listening with acceleration and jumps), register love level for 
each sound file and different users, automatic production of music lists 
based on predefined structures (example of a structure : 20 min 
classical and quiet, 20 min children songs, 30 minutes South American 
music, ... extra sound files like short bird songs being inserted 
randomly here and there, I am thinking of a meta-language to describe 
such a structure) taking into account love level and how long it has not 
been heard, ...

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


Re: '_[1]' in .co_names using builtin compile() in Python 2.6

2013-11-27 Thread Steven D'Aprano
On Wed, 27 Nov 2013 11:40:52 -0800, magnus.ly...@gmail.com wrote:

> What I'm really after, is to check that python expressions embedded in
> text files are: - well behaved (no syntax errors etc) - don't
> accidentally access anything it shouldn't - I serve them with the values
> they need on execution

If you are trying to get safe execution of untrusted code in Python, you 
should read this recent thread from the Python core developers:

https://mail.python.org/pipermail/python-dev/2013-November/130132.html


Probably the only way to securely sandbox untrusted Python code is to use 
operating system level security (such as a chroot jail) or an 
implementation such as PyPy which has been designed from the beginning to 
be sandboxed -- and even that may simply mean that nobody has broken out 
of PyPy's sandbox *yet*.

Looking back at your example:

compile('sin(5) * cos(6)', '', 'eval').co_names

I'm not sure I understand why you inspect the co_names. What does that 
give you? You can tell that there are no syntax errors just by compiling 
it, if there are syntax errors it will raise SyntaxError.

I would pre-process the string before compiling and disallow *anything* 
containing "eval", "exec", or underscore. I'd also apply a limit to the 
total length of the string. That doesn't necessarily rule out a hostile 
user running arbitrary code, but it does make it harder.

Also, when you execute the compiled code, don't do this:

eval(code)  # No!

Instead, provide an explicit globals and locals namespace:

safe_ish_namespace = {'__builtins__': None}
eval(code, safe_ish_namespace)


Again, this increases the barrier to somebody hacking out of your sandbox 
without ruling it out altogether.

Good luck!


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


Re: How to determine whether client and server are on the same host

2013-11-27 Thread Malte Forkel
Am 27.11.2013 00:37, schrieb Gregory Ewing:
>
> What would happen if you tried the file-based method when
> it wasn't a local connection? Is there a danger of it
> "succeeding" on the wrong machine and damaging something?
> 
I have been thinking about that.

There is a slight risk that the client might be talking to a server on a
remote host while there is a similar server running on the local
machine. That (local) server then might use a file by the same name as
the file on the remote server the client wants to operate on. In that
(unlikely) case I would mix up things big time...



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


Re: How to determine whether client and server are on the same host

2013-11-27 Thread Malte Forkel
Am 27.11.2013 01:14, schrieb Michael Torrie:
> 
> I believe socket.getsockname() can return the IP address of the
> connecting client if you're using standard tcp/ip sockets.
> 
The way you describe it, it sounds like it would require a change to the
server. I can only modify the client. I'll try to find out how I could
use that on the client.



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


Re: How to determine whether client and server are on the same host

2013-11-27 Thread Malte Forkel
Am 27.11.2013 00:15, schrieb Chris Angelico:
> Since the server runs Linux, inability to run /sbin/ifconfig could
> safely be interpreted as "we're not running on the server". But I
> think this actually gains little over "is there a file called
> /tmp/_this_is_malte_forkel_on_his_server_9515343_", which would be
> fairly safe.
> 
The server runs Linux, the client might be used under Windows as well.

I can't modify the server. So there is no way to create a file
/tmp/_this_is_malte_forkel_on_his_server_9515343 or to check whether it
exists on the server side.


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


Re: Recursive generator for combinations of a multiset?

2013-11-27 Thread Oscar Benjamin
On 27 November 2013 10:25, John O'Hagan  wrote:
> On Tue, 26 Nov 2013 10:33:06 +
> Oscar Benjamin  wrote:
>
> I simplified it a bit more to this:
>
> def word_sequences(prepend, target, subwords):
> """subwords is a list of lists of subwords grouped by length,
> in order of length"""
> for words in subwords:
> for word in words:
> recurse_target = subset_subtract(target, word)
> if recurse_target:
> yield from word_sequences(prepend + ' ' + word,
> recurse_target, subwords[:len(recurse_target)])
> elif recurse_target == '':
> yield prepend + ' ' + word
>
> with just one function to do the subset testing:
>
> def subset_subtract(target, word):
> for i in word:
> if i in target:
> target = target.replace(i, '' ,1)
> else:
> return
> return target
>
> Speed-wise it is somewhat faster than any of my non-duplicate-producing
> attempts, but still way slower than the current champion, the redundant
> cartesian product-only version.
>
> However, because it iterates through all the remaining words on each
> recursion, it seems to produce n! of each unique result, where n in the
> number of words in the result, so this is the new champion as far as
> redundancy is concerned. I'll keep working on it, the totally
> different approach is interesting.

Whoops, I guess this is what happens when you send untested
(pseudo-)code out. It needs an outer helper function that can do
something like:

def word_sequences_top(target, subwords):
for word in copy(subwords):
recurse_target = multiset_subtrace(target,word)
yield from word_sequences(words, recurse_target, subwords)
remove_word_from_list(word, subwords)

This way we yield all matches involving the word once and then go on
to all matches that don't include the word.

Also the partition length logic from your original version can be used
in word_sequences to prune recursion branches.


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


Re: tkinter bug on mac maverick python 3.3.3

2013-11-27 Thread Terry Reedy

On 11/27/2013 1:44 PM, Travis Griggs wrote:


Or are you proposing that Tcl/Tk be moved out of the python core
distro, and instead delivered as a separate package?


AFAIK, TK is only distributed with the core on Windows, where there is 
no package manager to easily install it. It is required for the turtle 
and idle modules.



If *this* is
your proposal, I wholeheartedly agree. Just the other day, I was
working on putting python3 on a beaglebone black (similar to a
raspberry pi). It built OK, but I had to ignore lots of warnings
about Tcl/Tk not working, which of course was a “duh”.


Were you running the test suite? Perhaps you can customize the list of 
expect skips for your particular system. But I am not sure how.



I was surprised, that among some, there’s a sentiment that python
core MUST include it. Which was interesting. One of the core
principles of this language is all about modules and modularity. Why
can’t the Tcl/Tk module be the same as numpy and scipy and many of
the other widely installed-after-the-fact-as-appropriate packages?


Nothing in the stdlib uses numpy. It is not a beginner package. Turtle 
and Idle are. Idle is really necessary, at least for many beginners, on 
Windows. Teachers and beginners appreciate being able to load and go.


--
Terry Jan Reedy


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


Re: python for everyday tasks

2013-11-27 Thread Chris Angelico
On Thu, Nov 28, 2013 at 5:05 AM, Pavel Volkov  wrote:
> "Got a switch statement? The Python translation is a hash table, not a bunch
> of if-then statments. Got a bunch of if-then's that wouldn't be a switch
> statement in Java because strings are involved? It's still a hash table. "

I actually disagree. There is no Python translation for a switch
statement. What you have is two ways (if/elif and dict lookup) to
implement what in other languages is often implemented with a switch.
Often, switch is used when a dispatch table is the ideal solution; but
other times, neither if/elif nor a dispatch table will perfectly cover
what you're trying to do, and it's not right to "translate" the switch
into a dict lookup. The right thing to do is to go back to what's
actually trying to be solved, and solve that.

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


Re: Access database - GUI - Python - I need architectural advice

2013-11-27 Thread Chris Angelico
On Thu, Nov 28, 2013 at 8:35 AM, jm.almeras  wrote:
> Access, and more generally VB, is excellent for building the GUI (forms,
> widgets etc.). Python is a great for coding, and it comes with high quality
> libraries... Does anyone have any suggestions as to how I can build my
> database app with nice Access-like GUI, and programming with Python ?

If you're working with Python, why work with Access? Use one of the
free databases (eg PostgreSQL if you want something heavy-duty, or
SQLite to avoid installing anything), use a cross-platform GUI toolkit
like Tk (tkinter), GTK, wx (wxPython), or similar, and have your
program not care whether it's on Windows, Linux, or OS/2. (Yeah, you
can support OS/2 if you feel like it!!)

There are GUI builders for several of the toolkits Python supports.
Glade works for GTK, and Google tells me about a thing called wxGlade,
which presumably is the equivalent for wx. Personally, though, I don't
use them; I create my user interfaces in code, which I find easier to
work with. Give 'em a try, but don't be too scared off; it's actually
really easy to create a good cross-platform window with a modern
toolkit. Effectively, what you say is something like:

Give me a window. Lay it out with a vertical box.
Create a scrollable widget
Put a drawing area into my scrollable widget.
Put the scrollable widget onto the window.
Put an entry field into my window.
Create a horizontal button box.
Put a button "Hello" into my button box.
Put a button "Goodbye" into my button box.
Put the button box onto the window.

That's more or less how my GTK window creation works. It reads nicely
in the source code, it looks like the window's layout. Plays well with
source control, too, which a lot of GUI builder output doesn't.

The builders are there if you want them. Try without, try with, see
what suits your hand.

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


mush 1.1 released! - Type-based dependency injection for scripts

2013-11-27 Thread Chris Withers

Hi All,

I'm very happy to announce the a new release of Mush, a light weight 
dependency injection framework aimed at enabling the easy testing and 
re-use of chunks of code that make up scripts.


This release rounds out a few rough edges after a few months of real 
world use:


- Runners can now be created from other Runners during instantiation or 
using the .extend() method as well as concatenation.


- @requires decorations can now be stacked

- Runners now have a .replace() method allowing you to replace a 
callable with a mock to make sure the callable is passed the right 
parameters.


For a worked example of how to use Mush to reduce the copy'n'paste in 
your scripts, please see here:


http://pythonhosted.org/mush/examples.html

Full docs are here:

http://pythonhosted.org/mush/

Downloads are here:

https://pypi.python.org/pypi/mush

Compatible with Python 2.6, 2.7, 3.2 and 3.3 on Linux, Mac OS X and Windows:

http://jenkins.simplistix.co.uk/view/mush/

Any problems, please give me a shout on the simplis...@googlegroups.com 
list!


cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
https://mail.python.org/mailman/listinfo/python-list


error

2013-11-27 Thread speen saba
Hello there,
   I am attending the lectures, and written the codes according to lectures. 
Professor's codes are working fine, but my one gives an error. I am new to 
python. Please it's a humble request, just be easy on me while making me 
understand about the code. It will be much appreciated.



import math
##points as lists
def addPoints(p1, p2):
r = []
r.append(p1[0]+p2[0])
r.append(p1[1]+p2[1])
return r

p = [1,2]
q = [3,1]
r = addPoints(p,q)
print r

 ##points as classes

class cartesianPoint():
  pass

cp1 = cartesianPoint()
cp2 = cartesianPoint()
cp1.x = 1.0
cp1.y = 2.0
cp2.x = 3.0
cp2.y = 1.0

def samePoint(p1,p2):
  return (p1.x == p2.x) and (p1.y == p2.y)

def printPoint(p):
  print '(' + str(p.x) + ', ' +str(p.y) + ')'

class polarPoint:
  pass
pp1 = polarPoint()
pp2 = polarPoint()
pp1.radius = 1.0
pp1.angle = 0
pp2.radius = 2.0
pp2.angle = math.pi / 4.0


class cPoint():
  def __init__(self,x,y):
self.x = x
self.y = y
self.radius = math.sqrt(self.x*self.x + self.y*self.y)
self.angle = math.atan2(self.y, self.x)
  def cartesian(self):
return (self.x, self.y)
  def polar(self):
return (self.radius, self.angle)
  def __str__(self):
return '(' + str(self.x) + ', ' + str(self.y) + ')'
  def __cmp__(self,other):
return (self.x == other.x) and (self.y == other.y)


And below is the error. Evrything works fine untill class polar point, but when 
I try to pick point (instance) p in the list i.e x,y (1,2,3,1). It does not 
work. I mean p.x gets the error where it should give me the value of x. And i 
know this error will go all the way down to def__cmp if i dont fic it from top.


   >>> p.x

Traceback (most recent call last):
  File "", line 1, in 
p.x
AttributeError: 'list' object has no attribute 'x'
>>> p.y

Traceback (most recent call last):
  File "", line 1, in 
p.y
AttributeError: 'list' object has no attribute 'y'

Help will be much appreciated, thanks in advance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: error

2013-11-27 Thread Cameron Simpson
On 27Nov2013 16:37, speen saba  wrote:
> Hello there,
>I am attending the lectures, and written the codes according to lectures. 
> Professor's codes are working fine, but my one gives an error. I am new to 
> python. Please it's a humble request, just be easy on me while making me 
> understand about the code. It will be much appreciated.
> 

Well, right at the top of your code you set "P" to be a list:

> p = [1,2]

Are you sure you don't want to print pp1.x or pp2.x or cp1.x or cp2.x?

Cheers,
-- 
Cameron Simpson 

Achilles, a PHD student, had to choose between having a passionate but poor
life as an academic or a dull but rich life as a IT professional yuppie.
Legend has it that he chose poverty.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread alex23

On 27/11/2013 7:12 PM, Antoon Pardon wrote:

However that there is no perfect solution doesn't imply
we can't expect some effort from those with english as a mother
tongue to search for ways in which to express themselves that are
more likely to be understood by those who had to learn english
as a foreign language than just to use their local idiom/dialect.


How do you expect people to know they're using a local idiom?

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


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Roy Smith
In article , alex23  
wrote:

> On 27/11/2013 7:12 PM, Antoon Pardon wrote:
> > However that there is no perfect solution doesn't imply
> > we can't expect some effort from those with english as a mother
> > tongue to search for ways in which to express themselves that are
> > more likely to be understood by those who had to learn english
> > as a foreign language than just to use their local idiom/dialect.
> 
> How do you expect people to know they're using a local idiom?

Look it up in Urban Dictionary and Bob's your uncle.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Got a Doubt ! Wanting for your Help ! Plz make it ASAP !

2013-11-27 Thread Tim Chase
On 2013-11-27 20:08, Roy Smith wrote:
> > How do you expect people to know they're using a local idiom?
> 
> Look it up in Urban Dictionary and Bob's your uncle.

I thought that's how one could tell if it was an *inappropriate*
idiom.  As a matter of fact, I'm surprised that "Bob's your uncle"
doesn't have more inappropriate definitions on UD.  :-P

-tkc



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


Re: Access database - GUI - Python - I need architectural advice

2013-11-27 Thread Mark Lawrence

On 28/11/2013 01:05, Dennis Lee Bieber wrote:

Not quite as friendly, and sort of a moving target: Look at Dabo [the
creators' goal was to create a Python equivalent of Visual FoxPro].



At http://www.dabodev.com/ and mailing list at gmane.comp.python.dabo.users.

--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Python project

2013-11-27 Thread ngangsia akumbo
Hi everyone

I a beginner in python. The first project is to build an online city guide 
start with my own city. I will need some support on where to get started.

support like where to find the required resources which will help me, 
libraries, tutorials on similar project etc. 

Please thanks for you understanding. Hope to here from you.


Richard

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


Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Victor Hooi
Hi,

I'm running pep8 across my code, and getting warnings about my long lines (> 80 
characters).

I'm wonder what's the recommended way to handle the below cases, and fit under 
80 characters.

First example - multiple context handlers:

with open(self.full_path, 'r') as input, open(self.output_csv, 
'ab') as output:

and in my case, with indents, the 80-character marks is just before the ending 
"as output".

What's the standard recognised way to split this across multiple lines, so that 
I'm under 80 characters?

I can't just split after the "as input," as that isn't valid syntax, and 
there's no convenient parentheses for me to split over.

Is there a standard Pythonic way?

Second example - long error messages:

self.logger.error('Unable to open input or output file - %s. Please 
check you have sufficient permissions and the file and parent directory exist.' 
% e)

I can use triple quotes:

self.logger.error(
"""Unable to open input or output file - %s. Please check you
have sufficient permissions and the file and parent directory
exist.""" % e)

However, that will introduce newlines in the message, which I don't want.

I can use backslashes:

self.logger.error(
'Unable to open input or output file - %s. Please check you\
have sufficient permissions and the file and parent directory\
exist.' % e)

which won't introduce newlines.

Or I can put them all as separate strings, and trust Python to glue them 
together:

self.logger.error(
'Unable to open input or output file - %s. Please check you'
'have sufficient permissions and the file and parent directory'
'exist.' % e)

Which way is the recommended Pythonic way?

Third example - long comments:

""" NB - We can't use Psycopg2's parametised statements here, as
that automatically wraps everything in single quotes.
So s3://my_bucket/my_file.csv.gz would become 
s3://'my_bucket'/'my_file.csv.gz'.
Hence, we use Python's normal string formating - this could
potentially exposes us to SQL injection attacks via the config.yaml
file.
I'm not aware of any easy ways around this currently though - I'm
open to suggestions though.
See

http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
for further information. """

In this case, I'm guessing a using triple quotes (""") is a better idea with 
multi-line comments, right?

However, I've noticed that I can't seem to put in line-breaks inside the 
comment without triggering a warning. For example, trying to put in another 
empty line in between lines 6 and 7 above causes a warning.

Also, how would I split up the long URLs? Breaking it up makes it annoying to 
use the URL. Thoughts?

Cheers,
Victor
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Victor Hooi
Hi,

Also, forgot two other examples that are causing me grief:

cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
[[os.path.relpath(filename, foobar_input_folder)] for 
filename in filenames])

I've already broken it up using the parentheses, not sure what's the tidy way 
to break it up again to fit under 80? In this case, the 80-character mark is 
hitting me around the "for filename" towards the end.

and:

if os.path.join(root, file) not in 
previously_processed_files and os.path.join(root, file)[:-3] not in 
previously_processed_files:

In this case, the 80-character mark is actually partway through "previously 
processed files" (the first occurrence)...

Cheers,
Victor

On Thursday, 28 November 2013 12:57:13 UTC+11, Victor Hooi  wrote:
> Hi,
> 
> 
> 
> I'm running pep8 across my code, and getting warnings about my long lines (> 
> 80 characters).
> 
> 
> 
> I'm wonder what's the recommended way to handle the below cases, and fit 
> under 80 characters.
> 
> 
> 
> First example - multiple context handlers:
> 
> 
> 
> with open(self.full_path, 'r') as input, open(self.output_csv, 
> 'ab') as output:
> 
> 
> 
> and in my case, with indents, the 80-character marks is just before the 
> ending "as output".
> 
> 
> 
> What's the standard recognised way to split this across multiple lines, so 
> that I'm under 80 characters?
> 
> 
> 
> I can't just split after the "as input," as that isn't valid syntax, and 
> there's no convenient parentheses for me to split over.
> 
> 
> 
> Is there a standard Pythonic way?
> 
> 
> 
> Second example - long error messages:
> 
> 
> 
> self.logger.error('Unable to open input or output file - %s. 
> Please check you have sufficient permissions and the file and parent 
> directory exist.' % e)
> 
> 
> 
> I can use triple quotes:
> 
> 
> 
> self.logger.error(
> 
> """Unable to open input or output file - %s. Please check you
> 
> have sufficient permissions and the file and parent directory
> 
> exist.""" % e)
> 
> 
> 
> However, that will introduce newlines in the message, which I don't want.
> 
> 
> 
> I can use backslashes:
> 
> 
> 
> self.logger.error(
> 
> 'Unable to open input or output file - %s. Please check you\
> 
> have sufficient permissions and the file and parent directory\
> 
> exist.' % e)
> 
> 
> 
> which won't introduce newlines.
> 
> 
> 
> Or I can put them all as separate strings, and trust Python to glue them 
> together:
> 
> 
> 
> self.logger.error(
> 
> 'Unable to open input or output file - %s. Please check you'
> 
> 'have sufficient permissions and the file and parent 
> directory'
> 
> 'exist.' % e)
> 
> 
> 
> Which way is the recommended Pythonic way?
> 
> 
> 
> Third example - long comments:
> 
> 
> 
> """ NB - We can't use Psycopg2's parametised statements here, as
> 
> that automatically wraps everything in single quotes.
> 
> So s3://my_bucket/my_file.csv.gz would become 
> s3://'my_bucket'/'my_file.csv.gz'.
> 
> Hence, we use Python's normal string formating - this could
> 
> potentially exposes us to SQL injection attacks via the 
> config.yaml
> 
> file.
> 
> I'm not aware of any easy ways around this currently though - I'm
> 
> open to suggestions though.
> 
> See
> 
> 
> http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
> 
> for further information. """
> 
> 
> 
> In this case, I'm guessing a using triple quotes (""") is a better idea with 
> multi-line comments, right?
> 
> 
> 
> However, I've noticed that I can't seem to put in line-breaks inside the 
> comment without triggering a warning. For example, trying to put in another 
> empty line in between lines 6 and 7 above causes a warning.
> 
> 
> 
> Also, how would I split up the long URLs? Breaking it up makes it annoying to 
> use the URL. Thoughts?
> 
> 
> 
> Cheers,
> 
> Victor
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: error

2013-11-27 Thread Dave Angel
On Wed, 27 Nov 2013 16:37:37 -0800 (PST), speen saba 
 wrote:

p = [1,2]


And below is the error. Evrything works fine untill class polar 
point, but when I try to pick point (instance) p in the list i.e x,y 
(1,2,3,1). It does not work. I mean p.x gets the error where it 
should give me the value of x. And i know this error will go all the 
way down to def__cmp if i dont fic it from top.



   >>> p.x



Traceback (most recent call last):
  File "", line 1, in 
p.x
AttributeError: 'list' object has no attribute 'x'


You have 4 different implementations, but you're mixing the first 
with the others.  Since p is a list, you need to use list semantics,  
p [0] and p [1]. The .x attribute is not defined on a list.


--
DaveA

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


Re: Python project

2013-11-27 Thread Dave Angel
On Wed, 27 Nov 2013 17:43:27 -0800 (PST), ngangsia akumbo 
 wrote:
I a beginner in python. The first project is to build an online 
city guide start with my own city. I will need some support on where 
to get started.


Are you experienced in other languages,  in html? Is this your first 
time programming on Linux? Are you using Python 3.3? Are you 
constrained in what extra modules you may or must use?


Can you just build each page using some Web generator?

--
DaveA

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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ben Finney
Victor Hooi  writes:

> I'm running pep8 across my code, and getting warnings about my long
> lines (> 80 characters).

Great! Thank you for working to make your code readable by keeping lines
reasonably short.

> I'm wonder what's the recommended way to handle the below cases, and
> fit under 80 characters.

In general, I advise:

* Avoid line-end backslashes like the plague. Sometimes they're
  necessary, but try very hard to write code that doesn't need them.

* Break a long line on existing open-parenthesis syntax (whether parens,
  brackets, braces, triple-quotes); early in the line is better, so the
  reader doesn't need to scan a long way back to the next line.

* Ignore the code on the first line for aligning, and instead indent
  continuation lines to a consistent level (i.e. don't line up with
  some arbitrary character on the first line).

* Use eight-column indentation for continuation lines (to clearly
  distinguish from four-column block indentation).

> First example - multiple context handlers:

I haven't used multiples in the same statement yet, and I'm annoyed that
they simultaneously encourage long statements, and have no obvious way
to break on an open parenthesis syntax.

> with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as 
> output:

So in this case I don't see a way to avoid the ugly line-end backslash::

with \
open(self.full_path, 'r') as input, \
open(self.output_csv, 'ab') as output:

Or::

with open(self.full_path, 'r') as input, \
open(self.output_csv, 'ab') as output:

Both look terrible to me. I'd love to know of a way to avoid
backslashes, while still breaking the line at the comma.

> Second example - long error messages:
>
> self.logger.error('Unable to open input or output file - %s. Please check 
> you have sufficient permissions and the file and parent directory exist.' % e)

Break at the first open-paren. Use implicit string literal continuation
to break that literal across multiple lines inside the parens. Like so::

self.logger.error(
'Unable to open input or output file - %s.'
' Please check you have sufficient permissions'
' and the file and parent directory exist.'
% e)

Also, with such a large literal this is an ideal example of why named
parameters are preferable::

self.logger.error(
'Unable to open input or output file - {error}.'
' Please check you have sufficient permissions'
' and the file and parent directory exist.'.format(
error=e))

> Third example - long comments:
>
> """ NB - We can't use Psycopg2's parametised statements here, as
> that automatically wraps everything in single quotes.
> So s3://my_bucket/my_file.csv.gz would become 
> s3://'my_bucket'/'my_file.csv.gz'.
> Hence, we use Python's normal string formating - this could
> potentially exposes us to SQL injection attacks via the config.yaml
> file.
> I'm not aware of any easy ways around this currently though - I'm
> open to suggestions though.
> See
> 
> http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
> for further information. """

That's not syntactically a comment, and I don't think pretending
triple-quoted strings are comments is good practice. If nothing else,
you'll need a special case if you want to enclose something with
existing triple-quotes.

Better to use the standard Python comment style::

# NB - We can't use Psycopg2's parametised statements here, as
# that automatically wraps everything in single quotes.
# So s3://my_bucket/my_file.csv.gz would become 
s3://'my_bucket'/'my_file.csv.gz'.
# Hence, we use Python's normal string formating - this could
# potentially exposes us to SQL injection attacks via the config.yaml
# file.
# I'm not aware of any easy ways around this currently though - I'm
# open to suggestions though.
# See
# 
http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
# for further information.

which most programmer's text editors can do for you easily by applying
or un-applying comments to a whole selected region of lines with a
single command.

The comments then are correctly identified as comments by all your
programming tools, and you don't have to think about using a different
comment style depending on the content — the same style is used for all
comments.

> Also, how would I split up the long URLs? Breaking it up makes it
> annoying to use the URL. Thoughts?

I'd try very hard to find an equivalent URL that isn't so long :-) but
URLs in comments are a good example of a PEP 8 exception: if the line is
over 80 characters because it contains a long URL in a comment, that's
fine, as I'm not expecting the human reader to be scanning it carefully
like other text in the code.

Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Terry Reedy

On 11/27/2013 8:57 PM, Victor Hooi wrote:

[sorry if the re-wrapping mis-formats anything]


I'm running pep8


Ah yes, the module that turns PEP8 into the straightjacket it explicitly 
says it is not meant to be.



across my code,


We mostly to not change existing stdlib modules to conform unless they 
are being edited anyway for fixes and features.



and getting warnings about my long lines (> 80 characters).


You are free to ignore such



I'm wonder what's the recommended way to handle the below cases, and
fit under 80 characters.

First example - multiple context handlers:

with open(self.full_path, 'r') as input, open(self.output_csv, 'ab')
as output:

and in my case, with indents, the 80-character marks is just before
the ending "as output".

What's the standard recognised way to split this across multiple
lines, so that I'm under 80 characters?


I believe () does not work, so put \ after 'input,'.


I can't just split after the "as input," as that isn't valid syntax,
and there's no convenient parentheses for me to split over.

Is there a standard Pythonic way?

Second example - long error messages:

self.logger.error('Unable to open input or output file - %s. Please
check you have sufficient permissions and the file and parent
directory exist.' % e)

I can use triple quotes:

self.logger.error( """Unable to open input or output file - %s.
Please check you have sufficient permissions and the file and parent
directory exist.""" % e)

However, that will introduce newlines in the message, which I don't
want.

I can use backslashes:

self.logger.error( 'Unable to open input or output file - %s. Please
check you\ have sufficient permissions and the file and parent
directory\ exist.' % e)

which won't introduce newlines.

Or I can put them all as separate strings, and trust Python to glue
them together:


Such gluing in a documented feature and I think you can trust it to not 
disappear (for at least a decade ;-).



self.logger.error( 'Unable to open input or output file - %s. Please
check you' 'have sufficient permissions and the file and parent
directory' 'exist.' % e)

Which way is the recommended Pythonic way?

Third example - long comments:

""" NB - We can't use Psycopg2's parametised statements here, as that
automatically wraps everything in single quotes. So
s3://my_bucket/my_file.csv.gz would become
s3://'my_bucket'/'my_file.csv.gz'. Hence, we use Python's normal
string formating - this could potentially exposes us to SQL injection
attacks via the config.yaml file. I'm not aware of any easy ways
around this currently though - I'm open to suggestions though. See
http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error

for further information. """

In this case, I'm guessing a using triple quotes (""") is a better
idea with multi-line comments, right?


Either are ok.


However, I've noticed that I can't seem to put in line-breaks inside
the comment without triggering a warning. For example, trying to put
in another empty line in between lines 6 and 7 above causes a
warning.


Blank lines are normal in triple-quoted strings and recommended for 
docstrings longer than a line. So I am puzzled as to your problem.



Also, how would I split up the long URLs?


Don't if you can possibly avoid it.


Breaking it up makes it annoying to use the URL.


Yep.


Thoughts?


Some urls, especially long ones, have extra tracking/query junk that can 
be removed while still pointing to the page.



--
Terry Jan Reedy

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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ben Finney
Victor Hooi  writes:

> cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
> [[os.path.relpath(filename, foobar_input_folder)] for 
> filename in filenames])
>
> I've already broken it up using the parentheses

But now the continuation line indentation is needlessly dependent on the
content of the previous line.

Better to just use a standard additional indentation (I prefer 8 columns
to easily distinguish from block indentation) for any continuation
line::

cur.executemany(
"INSERT INTO foobar_foobar_files VALUES (?)", [
[os.path.relpath(filename, foobar_input_folder)]
for filename in filenames])

Notice that further continuation within an existing continuation doesn't
get another 8, but only another 4 columns. The whole statement is
already distinguished, I don't need to add huge amounts of further
indentation for that purpose.

> if os.path.join(root, file) not in previously_processed_files and 
> os.path.join(root, file)[:-3] not in previously_processed_files:

In cases where the line is long because of a long expression, enclosing
that expression in parens for clarification will then give you an easy
way to break for continuation lines::

if (
os.path.join(root, file) not in previously_processed_files
and os.path.join(root, file)[:-3] not in 
previously_processed_files):

But that's still some long lines, and your repeated use of a computed
value is an ideal opportunity to bind it to a convenience name for local
use::

file_path = os.path.join(root, file)
if (
file_path not in previously_processed_files
and file_path[:-3] not in previously_processed_files):

Not least because you're very likely going to use the value of
‘file_path’ yet again in the body of that ‘if’ block.

-- 
 \ “Two paradoxes are better than one; they may even suggest a |
  `\ solution.” —Edward Teller |
_o__)  |
Ben Finney

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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ned Batchelder

On 11/27/13 8:57 PM, Victor Hooi wrote:

Hi,

I'm running pep8 across my code, and getting warnings about my long lines (> 80 
characters).

I'm wonder what's the recommended way to handle the below cases, and fit under 
80 characters.


My recommendations usually amount to: write more statements, each 
shorter than what you have.



First example - multiple context handlers:

 with open(self.full_path, 'r') as input, open(self.output_csv, 
'ab') as output:

and in my case, with indents, the 80-character marks is just before the ending "as 
output".

What's the standard recognised way to split this across multiple lines, so that 
I'm under 80 characters?

I can't just split after the "as input," as that isn't valid syntax, and 
there's no convenient parentheses for me to split over.

Is there a standard Pythonic way?


The important thing in a with statement is that the assigned name will 
be closed (or otherwise exited) automatically.  The open call is just 
the expression used to assign the name.  The expression there isn't 
really important.  This looks odd, but works the same as what you have:


input = open(self.full_path)
output = open(self.output_csv, 'ab')
with input as input, output as output:
...

(Use different names for the two parts of the "as" clauses if you like.)



Second example - long error messages:

 self.logger.error('Unable to open input or output file - %s. 
Please check you have sufficient permissions and the file and parent directory 
exist.' % e)

I can use triple quotes:

 self.logger.error(
 """Unable to open input or output file - %s. Please check you
 have sufficient permissions and the file and parent directory
 exist.""" % e)

However, that will introduce newlines in the message, which I don't want.

I can use backslashes:

 self.logger.error(
 'Unable to open input or output file - %s. Please check you\
 have sufficient permissions and the file and parent directory\
 exist.' % e)

which won't introduce newlines.

Or I can put them all as separate strings, and trust Python to glue them 
together:

 self.logger.error(
 'Unable to open input or output file - %s. Please check you'
 'have sufficient permissions and the file and parent directory'
 'exist.' % e)

Which way is the recommended Pythonic way?


Use the separate strings, but don't forget the spaces:

self.logger.error(
"Unable to open input or output file - %s. Please check you "
"have sufficient permissions and the file and parent directory "
"exist." % e
)



Third example - long comments:

 """ NB - We can't use Psycopg2's parametised statements here, as
 that automatically wraps everything in single quotes.
 So s3://my_bucket/my_file.csv.gz would become 
s3://'my_bucket'/'my_file.csv.gz'.
 Hence, we use Python's normal string formating - this could
 potentially exposes us to SQL injection attacks via the config.yaml
 file.
 I'm not aware of any easy ways around this currently though - I'm
 open to suggestions though.
 See
 
http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-with-sql-query-parameter-causes-syntax-error
 for further information. """

In this case, I'm guessing a using triple quotes (""") is a better idea with 
multi-line comments, right?


As Ben pointed out, using an actual comment is best.


However, I've noticed that I can't seem to put in line-breaks inside the 
comment without triggering a warning. For example, trying to put in another 
empty line in between lines 6 and 7 above causes a warning.


I don't know what you mean about line-breaks causing warnings.

--Ned.



Also, how would I split up the long URLs? Breaking it up makes it annoying to 
use the URL. Thoughts?

Cheers,
Victor




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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Terry Reedy

On 11/27/2013 9:03 PM, Victor Hooi wrote:

Hi,

Also, forgot two other examples that are causing me grief:

 cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
 [[os.path.relpath(filename, foobar_input_folder)] for 
filename in filenames])

I've already broken it up using the parentheses, not sure what's the tidy way to break it 
up again to fit under 80? In this case, the 80-character mark is hitting me around the 
"for filename" towards the end.


add another linebreak, you are still inside the parens.


and:

 if os.path.join(root, file) not in 
previously_processed_files and os.path.join(root, file)[:-3] not in 
previously_processed_files:

In this case, the 80-character mark is actually partway through "previously 
processed files" (the first occurrence)...


You can add parens. In this case, factoring out the common subexpression 
and replaced the repeated long name does the job.

p = os.path.join(root, file)
ppf = previously_processed_files
if p not in ppf and p[:-3] not in ppf:

[snip previous post]

--
Terry Jan Reedy

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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Steven D'Aprano
On Thu, 28 Nov 2013 13:47:22 +1100, Ben Finney wrote:


>> Third example - long comments:
>>
>> """ NB - We can't use Psycopg2's parametised statements here, as
>> that automatically wraps everything in single quotes. So
>> s3://my_bucket/my_file.csv.gz would become
>> s3://'my_bucket'/'my_file.csv.gz'. Hence, we use Python's normal
>> string formating - this could potentially exposes us to SQL
>> injection attacks via the config.yaml file. I'm not aware of any
>> easy ways around this currently though - I'm open to suggestions
>> though.
>> See
>> http://stackoverflow.com/questions/9354392/psycopg2-cursor-execute-
with-sql-query-parameter-causes-syntax-error
>> for further information. """
> 
> That's not syntactically a comment, and I don't think pretending
> triple-quoted strings are comments is good practice. If nothing else,
> you'll need a special case if you want to enclose something with
> existing triple-quotes.

The CPython core devs disagree with you. Using bare strings for comments 
is supported by Python, and the compiler strips them out at compile-time:


steve@runes:~$ python3.3
Python 3.3.0rc3 (default, Sep 27 2012, 18:31:58) 
[GCC 4.4.5] on linux
Type "help", "copyright", "credits" or "license" for more information.
py> import dis
py> dis.dis('x = 1; """Comment"""; y = 1')
  1   0 LOAD_CONST   0 (1) 
  3 STORE_NAME   0 (x) 
  6 LOAD_CONST   0 (1) 
  9 STORE_NAME   1 (y) 
 12 LOAD_CONST   1 (None) 
 15 RETURN_VALUE 
py> 


I'm on the fence on this one. I can it's useful; but it's also 
inconsistent with docstrings. And I wonder why other "dead objects" 
aren't also dropped:

py> dis.dis('x = 1; {23: "a", "b": 42}; y = 1')
  1   0 LOAD_CONST   0 (1) 
  3 STORE_NAME   0 (x) 
  6 BUILD_MAP2 
  9 LOAD_CONST   1 ('a') 
 12 LOAD_CONST   2 (23) 
 15 STORE_MAP
 16 LOAD_CONST   3 (42) 
 19 LOAD_CONST   4 ('b') 
 22 STORE_MAP
 23 POP_TOP  
 24 LOAD_CONST   0 (1) 
 27 STORE_NAME   1 (y) 
 30 LOAD_CONST   5 (None) 
 33 RETURN_VALUE 


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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ned Batchelder

On 11/27/13 9:03 PM, Victor Hooi wrote:

Hi,

Also, forgot two other examples that are causing me grief:

 cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
 [[os.path.relpath(filename, foobar_input_folder)] for 
filename in filenames])

I've already broken it up using the parentheses, not sure what's the tidy way to break it 
up again to fit under 80? In this case, the 80-character mark is hitting me around the 
"for filename" towards the end.


file_values = [
(os.path.relpath(filename, foobar_input_folder),)
for filename in filenames
]
cur.executemany(
"INSERT INTO foobar_foobar_files VALUES (?)",
file_values
)



and:

 if os.path.join(root, file) not in 
previously_processed_files and os.path.join(root, file)[:-3] not in 
previously_processed_files:

In this case, the 80-character mark is actually partway through "previously 
processed files" (the first occurrence)...


full_file = os.path.join(root, file)
full_not_in = full_file not in previously_processed_files
tail_not_in = full_file[:-3] not in previously_processed_files
if full_not_in and tail_not_in:
...

This has the advantage of naming these complex intermediate conditions, 
and giving you natural places to write comments explaining them.  Why 
[:-3] for example?


More, shorter, statements.

--Ned.



Cheers,
Victor




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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ben Finney
Ned Batchelder  writes:

> The important thing in a with statement is that the assigned name will
> be closed (or otherwise exited) automatically.  The open call is just
> the expression used to assign the name.  The expression there isn't
> really important.  This looks odd, but works the same as what you
> have:
>
> input = open(self.full_path)
> output = open(self.output_csv, 'ab')
> with input as input, output as output:
> ...

Excellent! That solves for me a significant irritation of the syntax for
multiple context managers. Thank you.

-- 
 \  “The most common way people give up their power is by thinking |
  `\   they don't have any.” —Alice Walker |
_o__)  |
Ben Finney

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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Neil Cerutti
tOn Wed, Nov 27, 2013 at 8:57 PM, Victor Hooi  wrote:
> with open(self.full_path, 'r') as input, open(self.output_csv, 
> 'ab') as output:

There are two nice and clean solutions for this.

I usually nest them.

with open(self.full_path, 'r') as input:
  with open(self.ouptut_csv, 'ab') as output:

I use a non-standard (shortened) indentation for nested with
statements, to save space. I don't feel bad about this at all; the
with statement's syntax makes me do it!

Otherwise, contextlib.ExitStack is another way to separate them.

with contextlib.ExitStack() as stack:
input = stack.enter_context(open(self.full_path, 'r'))
writer = csv.writer(stack.enter_context(open(self.output_csv)))

When working with a csv file I like how it removes the output
temporary file object variable, though if you needed it for some
reason you could keep it.

-- 
Neil Cerutti
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ben Finney
Steven D'Aprano  writes:

> On Thu, 28 Nov 2013 13:47:22 +1100, Ben Finney wrote:
> > [A triple-quoted string is] not syntactically a comment, and I don't
> > think pretending triple-quoted strings are comments is good
> > practice. If nothing else, you'll need a special case if you want to
> > enclose something with existing triple-quotes.
>
> The CPython core devs disagree with you. Using bare strings for comments 
> is supported by Python, and the compiler strips them out at
> compile-time:

That's not the same as disagreeing with what I said above. Triple-quoted
strings are not syntactically comments, they're still syntactically
treated as string literals. The implementation detail of them being
stripped out doesn't change that.

Also, “is supported by Python” doesn't mean the developers agree with
the practice.

-- 
 \  “Be careless in your dress if you must, but keep a tidy soul.” |
  `\  —Mark Twain, _Following the Equator_ |
_o__)  |
Ben Finney

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


Re: Python project

2013-11-27 Thread Johannes Findeisen
On Wed, 27 Nov 2013 17:43:27 -0800 (PST)
ngangsia akumbo wrote:

> Hi everyone

Hi,

> I a beginner in python. The first project is to build an online city guide 
> start with my own city. I will need some support on where to get started.

Take a look at the Django Web framework: https://www.djangoproject.com/

But, you really need to know a lot more then just a little piece of
Python. You need to know how web applications actually work and how to
implement a frontend using HTML, CSS etc.

Are you aware of this? Python will not do that for you.

> support like where to find the required resources which will help me, 
> libraries, tutorials on similar project etc. 

I believe you will not find libraries, tutorials etc. for creating a
"city guide" on the web. That is your job. You need to implement your
business idea yourself, using maybe Python.

Please give us more information about the state of your project and
what you have tried to implement and what you are missing... There are
lots of libraries available for Python doing most of the jobs you want
to have done.

Do you want us to explain how to build a web application?

Regards,
Johannes
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Ethan Furman

On 11/27/2013 06:59 PM, Ned Batchelder wrote:


The important thing in a with statement is that the assigned name will be 
closed (or otherwise exited) automatically.
The open call is just the expression used to assign the name.  The expression 
there isn't really important.  This looks
odd, but works the same as what you have:

 input = open(self.full_path)
 output = open(self.output_csv, 'ab')
 with input as input, output as output:
 ...

(Use different names for the two parts of the "as" clauses if you like.)


Or skip the `as` clauses all together:

  input = ...
  output = ...
  with input, output:
...

works just fine.  (At least on 2.7 where I tested it. ;)

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Steven D'Aprano
On Wed, 27 Nov 2013 17:57:13 -0800, Victor Hooi wrote:

> Hi,
> 
> I'm running pep8 across my code, and getting warnings about my long
> lines (> 80 characters).
> 
> I'm wonder what's the recommended way to handle the below cases, and fit
> under 80 characters.
> 
> First example - multiple context handlers:
> 
> with open(self.full_path, 'r') as input,
> open(self.output_csv, 'ab') as output:


if True:  # add indentation, just for the example's sake.
if True:
if True:
with (open(self.full_path, 'r') as input,
  open(self.output_csv, 'ab') as output):
do_this()
do_that()


 
> Second example - long error messages:
> 
> self.logger.error('Unable to open input or output file - %s.
> Please check you have sufficient permissions and the file
> and parent directory exist.' % e)

Long strings are always ugly :-(

But you can use implicit concatenation to make them a little less so.



if True:  # add indentation, just for the example's sake.
if True:
if True:
self.logger.error(
'Unable to open input or output file - %s.'
' Please check you have sufficient permissions and
' the file and parent directory exist.' 
% e)

Notice that my convention is to use a leading space in the strings when 
doing implicit concatenation, if possible. E.g.:

("Hello"
 " World!")

rather than:

("Hello "
 "World!")


> Third example - long comments:
> 
> """ NB - We can't use Psycopg2's parametised statements
> here, as that automatically wraps everything in single
> quotes. So s3://my_bucket/my_file.csv.gz would become
> s3://'my_bucket'/'my_file.csv.gz'. Hence, we use Python's
> normal string formating - this could potentially exposes us
> to SQL injection attacks via the config.yaml file.
> I'm not aware of any easy ways around this currently though
> - I'm open to suggestions though.
> See
> http://stackoverflow.com/questions/9354392/psycopg2-cursor-
execute-with-sql-query-parameter-causes-syntax-error
> for further information. """
> 
> In this case, I'm guessing a using triple quotes (""") is a better idea
> with multi-line comments, right?

*shrug* That's a matter of personal preference.


> However, I've noticed that I can't seem to put in line-breaks inside the
> comment without triggering a warning. For example, trying to put in
> another empty line in between lines 6 and 7 above causes a warning.
> 
> Also, how would I split up the long URLs? Breaking it up makes it
> annoying to use the URL. Thoughts?

I hate long URLs.

There's no good solution to long URLs. Just leave them as they come, and 
if your style checker allows you to flag an exception to the long-line 
rules, use it.


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


Re: Python and PEP8 - Recommendations on breaking up long lines?

2013-11-27 Thread Steven D'Aprano
On Wed, 27 Nov 2013 21:59:56 -0500, Ned Batchelder wrote:

> The important thing in a with statement is that the assigned name will
> be closed (or otherwise exited) automatically.  The open call is just
> the expression used to assign the name.  The expression there isn't
> really important.  This looks odd, but works the same as what you have:
> 
>  input = open(self.full_path)
>  output = open(self.output_csv, 'ab')
>  with input as input, output as output:
>  ...
> 
> (Use different names for the two parts of the "as" clauses if you like.)


That's really clever! Why didn't I think of that?


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


  1   2   >