Re: Beginner - GUI devlopment in Tkinter - Any IDE with drag and drop feature like Visual Studio?

2013-07-05 Thread Dave Cook
On 2013-07-04, Aseem Bansal  wrote:

> I have been searching all over google but couldn't find any IDE that
> has drag-and-drop feature for Python GUI development.

If you want something like Visual Studio, you could try IronPython:

http://www.ironpython.net/tools/

For any moderatley complex cross-platform application, I would suggest
moving on to wxPython, PyQt, or PySide (the latter two both based on
Qt).  Then try wxFormBuilder, wxGlade, or Qt Designer.

Generally, I'd prefer using PyQt/PySide, because Qt has the more
uniform API.  I use wxPython for commercial work because it had the
more liberal license at the time I started using it (PySide was not
available at that time).  

As far as features and maturity go, I think both wx and Qt are fairly
equal. Qt has become more popular for scientific work.

PyGtk and Glade are another option, particularly on Linux, though I
think wx or Qt still have better compatibility with Win32 or OS X.  

For OS X only, PyObjC and XCode are an option.

While they are a tool that can save time, it's pretty easy to bump
into the limitations of form designers.  It's best to lower ones
expectations about how much coding can be offloaded onto a form
designer.

http://sourceforge.net/projects/wxformbuilder/
http://qt-project.org/doc/qt-4.8/designer-manual.html
http://wxglade.sourceforge.net/

> I came across somewhere that eclipse's pydev plugin can be used but
> couldn't find anything on its website.

The only form builders for Eclipse that I'm aware of are for Swing or
SWT.  You would need to use Jython (if you want to stick with a Python
implementation) to interface with these.

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


Re: How is this evaluated

2013-07-05 Thread Chris Angelico
On Fri, Jul 5, 2013 at 11:41 AM, Steven D'Aprano
 wrote:
> If you know C, that's like:
>
> ?(condition-being-tested, value-if-true, value-if-false)

Or to be precise:

condition-being-tested ? value-if-true : value-if-false

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Νίκος Gr33k  writes:

> try:
>   host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
> except Exception as e:
>   host = "Reverse DNS Failed"
>
> How can the above code not be able to reeverse dns any more and it
> falls back to  the failed string?

The only way to know is actually printing out the exception, either to
stderr, or better using the logging facility, as I suggested.

FYI, your code above is (almost) exactly equivalent to the simpler

try:
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
except:
host = "Reverse DNS Failed"

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 10:06 πμ, ο/η Lele Gaifax έγραψε:

 try:
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
 except:
host = "Reverse DNS Failed"



Yes i uses to had it like that, until i was looking for ways to make it 
hold the error


except Exception as e:
print( e )
host = e

but print( e ) in the way i used to had it doesn't print out the error, 
it instead gives an internal server error on browser.


I must somehow take a look at the error to understand why every visitor 
i have gets UnResolved, but how since prints fails?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 9:55 πμ, ο/η Lele Gaifax έγραψε:

Ferrous Cranus  writes:


host =  gethostbyaddr() or "UnResolved"

This will return the first argument that define the evaluation as
being true or untrue.

if function returns false the the 2nd argument.
Nut if the function gives an exception will the condition return the
2nd argument or will the program fail?

I was udner the impression that i could avoid error handling  inside
try/excepts by utilizing "or".


No, you had the wrong impression. Why don't you simply invoke the Python
interpreter and try things out with that??

 >>> a = 1/0 or 100
 Traceback (most recent call last):
   File "", line 1, in 
 ZeroDivisionError: division by zero
 >>> a
 Traceback (most recent call last):
   File "", line 1, in 
 NameError: name 'a' is not defined
 >>> a = 0/1 or 100
 >>> a
 100
 >>>


Thank you Lele, i wanted to but i had no idea how to test it.
Your devision by zero is very smart thing to test!

So it proves that a condition cannot be evaluation as truthy or falsey 
if one of the operators is giving out an exception.

Thank you.


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Illegal suggestions on python list

2013-07-05 Thread Chris Angelico
On Fri, Jul 5, 2013 at 4:47 PM, zoom  wrote:
> Probably...
>
> http://en.wikipedia.org/wiki/Aaron_Swartz#JSTOR

Things are always more complicated than any simple Usenet post can do
justice to, and it'll often come down to who has the biggest legal
budget. Since he was never actually found guilty (or so a cursory
reading of that Wikipedia page suggests), that can't really be taken
as proof; anyone can be charged with anything if someone can trump up
some plausible evidence (no, I'm not completely cynical, why do you
ask?), but that doesn't make walking and chewing gum at the same time
a crime.

Anyway, this is all technicalities; it's still unwise to advocate
legally and/or morally shady actions in public. You never know what
people will find back years later.

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


Re: Illegal suggestions on python list

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 10:18 πμ, ο/η Chris Angelico έγραψε:

On Fri, Jul 5, 2013 at 4:47 PM, zoom  wrote:

Probably...

http://en.wikipedia.org/wiki/Aaron_Swartz#JSTOR


Things are always more complicated than any simple Usenet post can do
justice to, and it'll often come down to who has the biggest legal
budget. Since he was never actually found guilty (or so a cursory
reading of that Wikipedia page suggests), that can't really be taken
as proof; anyone can be charged with anything if someone can trump up
some plausible evidence (no, I'm not completely cynical, why do you
ask?), but that doesn't make walking and chewing gum at the same time
a crime.

Anyway, this is all technicalities; it's still unwise to advocate
legally and/or morally shady actions in public. You never know what
people will find back years later.

ChrisA



Chris i just wanted to offer some help to you guys because i received a 
lot for help here myself.


It was my way to contribute to the people here.
Of course i shouldn't have proposed that because yes its like ripping 
the company off.


Of course we all know that a serial/patch/keygen/crack can be found for 
this great edit very easily on warez or torrentz sites so it was like a 
common secret to all of us.


I just wanted to make things easy and felt like helping.
I wont post something like that again though.


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Benjamin Kaplan
On Jul 5, 2013 12:12 AM, "Lele Gaifax"  wrote:
>
> Νίκος Gr33k  writes:
>
> > try:
> >   host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
> > except Exception as e:
> >   host = "Reverse DNS Failed"
> >
> > How can the above code not be able to reeverse dns any more and it
> > falls back to  the failed string?
>
> The only way to know is actually printing out the exception, either to
> stderr, or better using the logging facility, as I suggested.
>
> FYI, your code above is (almost) exactly equivalent to the simpler
>
> try:
> host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
> except:
> host = "Reverse DNS Failed"
>
> ciao, lele.
>

They aren't equivalent. "except Exception" won't catch KeyboardInterrupt or
SystemExit or a few others that you really don't want to catch in a generic
error handler. You should almost never have a bare except.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Important features for editors

2013-07-05 Thread Terry Reedy

On 7/4/2013 2:52 PM, Ferrous Cranus wrote:


Like you never downloaded serials/keygens/patch/cracks for warez and
torrents websites.


Morality aside, why would I? Today I bought 8 games on GOG.com for about 
$22 - drm and virus free and easy download and install. If I get 10 
hours of fun from 2 of them, I'll be happy. This is not to mention free 
Python and LibreOffice as my primary work programs - suppported by hg, 
TortoiseHg, 7zip, and others.


--
Terry Jan Reedy

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


Fwd: Important features for editors

2013-07-05 Thread Göktuğ Kayaalp
Programmability comes to my mind, before anything else.  I'd suggest
to find out about designs of Emacs and Vi(m).

On Thu, Jul 4, 2013 at 10:32 AM, cutems93  wrote:
> I am researching on editors for my own reference. I found that each of them 
> has some features that other don't, but I am not sure which features are 
> significant/necessary for a GOOD editor. What features do you a good editor 
> should have? Keyboard shortcuts? Extensions?
>
> Thanks!
> Min
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Illegal suggestions on python list

2013-07-05 Thread Chris Angelico
On Fri, Jul 5, 2013 at 5:23 PM, Νίκος Gr33k  wrote:
> Chris i just wanted to offer some help to you guys because i received a lot
> for help here myself.

Hey guys, I really appreciate what you've done, so here's what I'll
do: I'm robbing the Last National Bank on Tuesday, and you can all
join me!

> It was my way to contribute to the people here.
> Of course i shouldn't have proposed that because yes its like ripping the
> company off.
>
> Of course we all know that a serial/patch/keygen/crack can be found for this
> great edit very easily on warez or torrentz sites so it was like a common
> secret to all of us.

Actually no, I don't know that that's available. And if we all know,
why bother saying it?

Mind you, I can't honestly say that I've *never* bypassed a pay-for
system. But I would not suggest it about something that I like and
want to encourage (as you seem to with this editor), and if I'm going
to say something in public, it's going to be worded carefully -
something like this:

http://rosuav.blogspot.com.au/2012/07/ea-games-dont-buy-from-them.html

And that's the strongest I'll ever get on the subject, and only there
because I have a VERY strong dislike for their actions.

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


Re: Important features for editors

2013-07-05 Thread Νίκος Gr33k

Στις 4/7/2013 10:59 πμ, ο/η Νίκος έγραψε:

Στις 4/7/2013 10:32 πμ, ο/η cutems93 έγραψε:

I am researching on editors for my own reference. I found that each of
them has some features that other don't, but I am not sure which
features are significant/necessary for a GOOD editor. What features do
you a good editor should have? Keyboard shortcuts? Extensions?

Thanks!
Min


Download Sublime Text v3

Is a great editor



The only thing missing from this great editor is the ability to upload 
your python scripts to a remote web server.


They should embed a plugin  for that like Notepad's NPPFtp plugin.

Other that that its all complete, and it has help built-in system to 
help you with the syntax of the commands as you write them.


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


question please

2013-07-05 Thread bill papanastasiou
hello , good morning

how i can pùt one python file in website ?


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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Dave Angel

On 07/05/2013 02:51 AM, Νίκος Gr33k wrote:




Please help because i just happened to noticed that after having this code:

try:
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
except Exception as e:
 host = "Reverse DNS Failed"


Don't ever catch a bare Exception class.  Make it more specific to the 
particular problem you're trying to suppress.


In particular, your previous problem with the utf-8 decoding will also 
be caught by the Exception class, so it'll get all lumped together as 
"Reverse DNS Failed".





all requests are being resolves, result to:


Reverse DNS Failed as you can see here:
http://superhost.gr/?show=log&page=index.html

How can the above code not be able to reeverse dns any more and it falls
back to  the failed string?



Since you've not made any progress with all the other suggestions, how 
about if you decompose this line into several, and see just which one is 
failing?  Maybe that'll tell you what's going on.  In general, 
suppressing an exception without knowing why it's firing is a huge mistake.


The line started as:

>  host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

refactor that to:
   remadd = os.environ('REMOVE_ADDR')
   tuple3 = socket.gethostbyaddr(remadd)
   host = tuple3[0]

and see which one throws the exception.  Then once you have that, 
examine the exact parameters that might be triggering the problem.  In 
particular, figure out the exact types and values for remadd and tuple3.


   print(type(remadd) + " : " + repr(remadd))

Of course, print itself won't work too well in a CGI environment.  But 
you must have solved that problem by now, either using log files or 
running the program excerpt in a regular console.



--
DaveA

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 10:50 πμ, ο/η Dave Angel έγραψε:


The line started as:

 >  host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

refactor that to:
remadd = os.environ('REMOVE_ADDR')
tuple3 = socket.gethostbyaddr(remadd)
host = tuple3[0]

and see which one throws the exception.  Then once you have that,
examine the exact parameters that might be triggering the problem.  In
particular, figure out the exact types and values for remadd and tuple3.

print(type(remadd) + " : " + repr(remadd))


I'am not sure how iam supposed to write this: i just tried this:


try:
remadd = os.environ('REMOVE_ADDR')
tuple3 = socket.gethostbyaddr(remadd)
host = tuple3[0]
except:
host = type(remadd) + " : " + repr(remadd)


but iam getting an internal server error.

I didnt print it as you said but its the same thing host var gets 
printed later on.


Now, why would this give an internal server error?

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


How to make this faster

2013-07-05 Thread Helmut Jarausch
Hi,

I have coded a simple algorithm to solve a Sudoku (probably not the first one).
Unfortunately, it takes 13 seconds for a difficult problem which is more than 
75 times slower
than the same algorithm coded in C++.
Is this to be expected or could I have made my Python version faster *** 
without *** sacrificing readability.
Profiling shows that the function find_good_cell is called (only) 45267 times 
and this take 12.9 seconds
CPU time (on a 3.2 GHz machine)

For your pleasure I include the full code below.

Many thanks for a hint,
Helmut


#!/usr/bin/python3
import numpy as np

Grid= np.zeros((9,9),dtype=int)
Row_Digits = np.asarray(np.zeros((9,10)),dtype=bool)
Col_Digits = np.asarray(np.zeros((9,10)),dtype=bool)
Sqr_Digits = np.asarray(np.zeros((9,10)),dtype=bool)

def Read_Sudoku(Input) :
  r= -1
  R_Cells= 81
  for line in Input :
line= line.strip()
if  len(line) == 0  or  line[0] == '#' : continue
r+= 1
for (c,ch) in enumerate(line) :
  if  ch == '_' : continue
  if not ch in "123456789_" :
raise ValueError("invalid character {0} in input line 
{1}".format(c,line))
  Sq_No= (r//3)*3+c//3
  d= int(ch)
  Grid[r,c]= d
  Row_Digits[r,d]= True
  Col_Digits[c,d]= True
  Sqr_Digits[Sq_No,d]= True
  R_Cells-= 1
  
  return R_Cells

def Print_Grid() :
  Sep = "+---+---+---#---+---+---#---+---+---+"
  SepK= "#"
  print(Sep)
  for r in range(9) :
print('|',end='')
for c in range(9) :
  d= Grid[r,c]
  print(" {0} {1}".format( str(d) if d>0 else ' ',
   '#' if (c+1)%3==0 and c>0 and c<8 else '|' ), 
end='')
print("\n{}".format(SepK if (r+1)%3==0 and r>0 and r<8 else Sep))


def find_good_cell() :
  Best= None
  minPoss= 10
  for r in range(9) :
for c in range(9) :
  if  Grid[r,c] > 0 : continue
  Sq_No= (r//3)*3+c//3
  Possibilities= 0
  for d in range(1,10) :
if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
Possibilities+= 1
  
  if ( Possibilities < minPoss ) :
minPoss= Possibilities
Best= (r,c)

  if minPoss == 0 : Best=(-1,-1)
  return Best

def Solve(R_Cells) :
  if  R_Cells == 0 :
print("\n\n++ S o l u t i o n ++\n")
Print_Grid()
return True

  r,c= find_good_cell()
  if r < 0 : return False
  Sq_No= (r//3)*3+c//3
  
  for d in range(1,10) :
if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
# put d into Grid
Grid[r,c]= d
Row_Digits[r,d]= True
Col_Digits[c,d]= True
Sqr_Digits[Sq_No,d]= True

Success= Solve(R_Cells-1)

# remove d again
Grid[r,c]= 0
Row_Digits[r,d]= False
Col_Digits[c,d]= False
Sqr_Digits[Sq_No,d]= False
  
if Success :
  Zuege.append((d,r,c))
  return True

  return False

from io import StringIO

Problem='''
_
_3_85
__1_2
___5_7___
__4___1__
_9___
5__73
__2_1
4___9
'''

Input= StringIO(Problem)
from time import process_time
R_Cells= Read_Sudoku(Input)
Print_Grid()
Zuege=[]
Start= process_time()
# import cProfile
# cProfile.run("Success = Solve(R_Cells)")
Success = Solve(R_Cells)
Stop= process_time()
print("after {} seconds:".format(Stop-Start))
if Success :
  print("\nZuege:")
  n=0
  for Z in reversed(Zuege) :
print("{0} -> ({1},{2})\t".format(Z[0],Z[1]+1,Z[2]+1),end='')
n+= 1
if n%5 == 0 :
  print()

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Dave Angel

On 07/05/2013 03:13 AM, Νίκος Gr33k wrote:

Στις 5/7/2013 10:06 πμ, ο/η Lele Gaifax έγραψε:

 try:
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
 except:
 host = "Reverse DNS Failed"



Yes i uses to had it like that, until i was looking for ways to make it
hold the error

except Exception as e:
 print( e )
 host = e

but print( e ) in the way i used to had it doesn't print out the error,
it instead gives an internal server error on browser.

I must somehow take a look at the error to understand why every visitor
i have gets UnResolved, but how since prints fails?




How have you been doing it all along?  Just open a console onto that 
server, start the appropriate version of Python interactively, and try 
the things we've been talking about.  If it fails the same way as within 
the cgi environmnet, you get full visibility.


Or if the problems cannot be recreated outside the cgi environment, use 
the log files you've been logging other problems into.  Or simply open a 
text file for writing, and add a file= keyword parameter to the print 
function call.


 print(repr(e), file=myfile)



--
DaveA

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


Re: Important features for editors

2013-07-05 Thread feedthetroll
Am Donnerstag, 4. Juli 2013 11:14:38 UTC+2 schrieb Νίκος Gr33k:
> ...
>> On 07/04/2013 03:59 AM, Νίκος wrote:
>>> ... 
>>> Download Sublime Text v3
>>> Is a great editor
>>> ... 
>
> If you guys want to use it i can send you a patch for it.
> I know its illegal thing to say but it will help you use it without 
> buying it.

Am Freitag, 5. Juli 2013 09:41:39 UTC+2 schrieb Νίκος Gr33k:
> [talkin about Sublime Text editor]
> The only thing missing from this great editor is the ability to upload 
> your python scripts to a remote web server.
> They should embed a plugin  for that like Notepad's NPPFtp plugin.

Oh, I'm sure they would have time/money to do so, if more people payed the 
license fees.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Dave Angel

On 07/05/2013 04:00 AM, Νίκος Gr33k wrote:

Στις 5/7/2013 10:50 πμ, ο/η Dave Angel έγραψε:


The line started as:

 >  host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

refactor that to:
remadd = os.environ('REMOVE_ADDR')
tuple3 = socket.gethostbyaddr(remadd)
host = tuple3[0]

and see which one throws the exception.  Then once you have that,
examine the exact parameters that might be triggering the problem.  In
particular, figure out the exact types and values for remadd and tuple3.

print(type(remadd) + " : " + repr(remadd))


I'am not sure how iam supposed to write this: i just tried this:


try:
 remadd = os.environ('REMOVE_ADDR')
 tuple3 = socket.gethostbyaddr(remadd)
 host = tuple3[0]
except:
 host = type(remadd) + " : " + repr(remadd)


but iam getting an internal server error.

I didnt print it as you said but its the same thing host var gets
printed later on.

Now, why would this give an internal server error?



I have no idea what causes an internal server error.  It's up to you to 
get the output of the expression to some location you can examine. 
Easiest way is to run those 3 lines directly on the server, not in the 
cgi environment.


But if you don't have any debugging tools, then STOP right now and build 
some.  Use logging, or redirect print, or do something that the server 
folks provide as debugging aids.  But just blindly guessing is 
ludicrous.  So also is throwing out clues by using a bare except.


Assigning that string to host makes no sense at all.  And neither does 
putting it in the except clause.  You want to get that string to YOUR 
eyes, not to the server who might get an internal server error.



--
DaveA

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


Re: How is this evaluated

2013-07-05 Thread Steven D'Aprano
On Fri, 05 Jul 2013 17:05:49 +1000, Chris Angelico wrote:

> On Fri, Jul 5, 2013 at 11:41 AM, Steven D'Aprano
>  wrote:
>> If you know C, that's like:
>>
>> ?(condition-being-tested, value-if-true, value-if-false)
> 
> Or to be precise:
> 
> condition-being-tested ? value-if-true : value-if-false


Oops. Sorry about that. I thought it looked wrong even as I was typing it.




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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread feedthetroll
Am Freitag, 5. Juli 2013 10:00:21 UTC+2 schrieb Νίκος Gr33k:
> ...
> I'am not sure how iam supposed to write this: i just tried this:
> try:
>   remadd = os.environ('REMOVE_ADDR')
>   tuple3 = socket.gethostbyaddr(remadd)
>   host = tuple3[0]
> except:
>   host = type(remadd) + " : " + repr(remadd)

Hey, if no one told you before:
You are allowed to read what other people suggest you to do, think about it  
and so correct obvious typos: 'REMO*V*E_ADDR'

By the way, my i cite:
Am Donnerstag, 4. Juli 2013 14:52:59 UTC+2 schrieb Νίκος Gr33k:
> ...
> '108.162.229.97' is the result of:
> print( ascii(os.environ['REMOTE_ADDR']) )

Am Donnerstag, 4. Juli 2013 16:48 UTC+2 schrieb Νίκος Gr33k:
> For me, socket.gethostbyaddr('108.162.229.97') raises socket.herror,
> which is also a subclass of OSError from Python 3.3 onwards.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: question please

2013-07-05 Thread Steven D'Aprano
On Fri, 05 Jul 2013 09:48:09 +0200, bill papanastasiou wrote:

> hello , good morning
> 
> how i can pùt one python file in website ?


The same way you would put any other file in a website.

Can you be more specific? What website do you want to put it on? Is it 
your website or somebody else's?


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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 11:35 πμ, ο/η Dave Angel έγραψε:

On 07/05/2013 04:00 AM, Νίκος Gr33k wrote:

Στις 5/7/2013 10:50 πμ, ο/η Dave Angel έγραψε:


The line started as:

 >  host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

refactor that to:
remadd = os.environ('REMOVE_ADDR')
tuple3 = socket.gethostbyaddr(remadd)
host = tuple3[0]

and see which one throws the exception.  Then once you have that,
examine the exact parameters that might be triggering the problem.  In
particular, figure out the exact types and values for remadd and tuple3.

print(type(remadd) + " : " + repr(remadd))


I'am not sure how iam supposed to write this: i just tried this:


try:
 remadd = os.environ('REMOVE_ADDR')
 tuple3 = socket.gethostbyaddr(remadd)
 host = tuple3[0]
except:
 host = type(remadd) + " : " + repr(remadd)


but iam getting an internal server error.

I didnt print it as you said but its the same thing host var gets
printed later on.

Now, why would this give an internal server error?



I have no idea what causes an internal server error.  It's up to you to
get the output of the expression to some location you can examine.
Easiest way is to run those 3 lines directly on the server, not in the
cgi environment.

But if you don't have any debugging tools, then STOP right now and build
some.  Use logging, or redirect print, or do something that the server
folks provide as debugging aids.  But just blindly guessing is
ludicrous.  So also is throwing out clues by using a bare except.

Assigning that string to host makes no sense at all.  And neither does
putting it in the except clause.  You want to get that string to YOUR
eyes, not to the server who might get an internal server error.


I don't think running it via 'cli' would help much, since its a 
cgi-script and ip addr function have no meaning calling them in plain 
our of a cgi environment but here it is:



Python 3.3.2 (default, Jun  3 2013, 16:18:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> remadd = os.environ('REMOVE_ADDR')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: '_Environ' object is not callable



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: question please

2013-07-05 Thread Dave Angel

On 07/05/2013 03:48 AM, bill papanastasiou wrote:

hello , good morning

how i can pùt one python file in website ?



Whose website?  If it's your own, log into the server, and use cp. Or if 
you're remote with ssh access, use scp.  And if you really have a bunch 
of files to remotely transfer, use rsync from your local staging area. 
It's smart enough to only transfer the files that are missing or changed.


The type of the file doesn't matter, unless you also have to convert 
line-endings from Windoze to Unix.  And if so, you should do it as a 
separate step, when you update your staging area.  So the copying of the 
file onto the website can be done as a simple binary transfer.


If none of this works, or doesn't make sense, then tell us a little more 
about this site, how it's being hosted, and what access you do have.  In 
some cases the answer is to hand a USB stick to the administrator down 
the hall.


--
DaveA

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 11:27 πμ, ο/η Dave Angel έγραψε:


Or if the problems cannot be recreated outside the cgi environment, use
the log files you've been logging other problems into.  Or simply open a
text file for writing, and add a file= keyword parameter to the print
function call.

  print(repr(e), file=myfile)


Yes you are correct, problem need to be recreated within the cgi env.

try:
remadd = os.environ('REMOTE_ADDR')
tuple3 = socket.gethostbyaddr(remadd)
host = tuple3[0]
except Exception as e:
host = repr(e)


http://superhost.gr/?show=log&page=index.html shows explicitly the same 
kind of error that python interpreter via cli gave me the same error!


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Dave Angel

On 07/05/2013 04:49 AM, Νίκος Gr33k wrote:



 


I don't think running it via 'cli' would help much, since its a
cgi-script and ip addr function have no meaning calling them in plain
our of a cgi environment but here it is:



No idea how to parse "have no meaning calling them in plain our of a cgi 
environment"





Python 3.3.2 (default, Jun  3 2013, 16:18:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> remadd = os.environ('REMOVE_ADDR')
Traceback (most recent call last):
   File "", line 1, in 
TypeError: '_Environ' object is not callable

But there were two problems with the code you faithfully copied from my 
earlier post.  One was already pointed out by feedthetroll, that I 
accidentally changed REMOTE_ADDR to REMOVE_ADDR.


The other one is perhaps more subtle; I replaced square brackets with 
parentheses.


So try again with:

 >>> import os
 >>> remadd = os.environ['REMOTE_ADDR']

I get an error:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.3/os.py", line 669, in __getitem__
value = self._data[self.encodekey(key)]
KeyError: b'REMOTE_ADDR'


but presumably your machine actually has such an environment variable.

Isn't that mistake something  you could easily have caught?  Or were you 
just blindly pasting my bugs without understanding what I was trying to 
do with refactoring?


Anyway, I can't see any reason why the rest of the sequence shouldn't 
behave identically from a terminal as it does in CGI.



--
DaveA

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 12:21 μμ, ο/η Dave Angel έγραψε:

On 07/05/2013 04:49 AM, Νίκος Gr33k wrote:



  


I don't think running it via 'cli' would help much, since its a
cgi-script and ip addr function have no meaning calling them in plain
our of a cgi environment but here it is:



No idea how to parse "have no meaning calling them in plain our of a cgi
environment"




Python 3.3.2 (default, Jun  3 2013, 16:18:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> remadd = os.environ('REMOVE_ADDR')
Traceback (most recent call last):
   File "", line 1, in 
TypeError: '_Environ' object is not callable


But there were two problems with the code you faithfully copied from my
earlier post.  One was already pointed out by feedthetroll, that I
accidentally changed REMOTE_ADDR to REMOVE_ADDR.

The other one is perhaps more subtle; I replaced square brackets with
parentheses.

So try again with:

  >>> import os
  >>> remadd = os.environ['REMOTE_ADDR']

I get an error:

Traceback (most recent call last):
   File "", line 1, in 
   File "/usr/local/lib/python3.3/os.py", line 669, in __getitem__
 value = self._data[self.encodekey(key)]
KeyError: b'REMOTE_ADDR'


but presumably your machine actually has such an environment variable.

Isn't that mistake something  you could easily have caught?  Or were you
just blindly pasting my bugs without understanding what I was trying to
do with refactoring?

Anyway, I can't see any reason why the rest of the sequence shouldn't
behave identically from a terminal as it does in CGI.


Yes i didnt see your typo and i have corrected it:


try:
remadd = os.environ('REMOTE_ADDR')
tuple3 = socket.gethostbyaddr(remadd)
host = tuple3[0]
except Exception as e:
host = repr(e)


Ima still receiving the same kind of erro as i did with cli as well.

You can view the error in the very first line here:

http://superhost.gr/?show=log&page=index.html


which yields: TypeError("'_Environ' object is not callable",)


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Coping with cyclic imports

2013-07-05 Thread Oscar Benjamin
On 5 July 2013 02:24, Cameron Simpson  wrote:
> On 04Jul2013 16:03, Oscar Benjamin  wrote:
> |
> | Is there some reason you're responding to a post from 5 years ago?
>
> Is there some reason not to, if no newer solutions are available?

No, I was genuinely curious. My way of accessing this
forum/newsgroup/mailing list doesn't give me a way to respond to very
old posts but others seem to do it every now and again. I see now that
if you're looking at an old thread in Google Groups (rather than e.g.
the python.org archives) it makes the thread seem more like a forum
than a newsgroup or a mailing list so that it's easy and seems more
natural to respond to old posts.


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


Re: How to make this faster

2013-07-05 Thread Fábio Santos
On 5 Jul 2013 09:29, "Helmut Jarausch"  wrote:
>
> Hi,
>
> I have coded a simple algorithm to solve a Sudoku (probably not the first
one).
> Unfortunately, it takes 13 seconds for a difficult problem which is more
than 75 times slower
> than the same algorithm coded in C++.
> Is this to be expected or could I have made my Python version faster ***
without *** sacrificing readability.
> Profiling shows that the function find_good_cell is called (only) 45267
times and this take 12.9 seconds
> CPU time (on a 3.2 GHz machine)

[Skipping to bottleneck]

> def find_good_cell() :

In this function you are accessing global variables a lot of times. Since
accessing globals takes much more time than accessing locals, I advise you
to assign them to local names, and use them.

>   Best= None
>   minPoss= 10
>   for r in range(9) :
> for c in range(9) :
>   if  Grid[r,c] > 0 : continue
>   Sq_No= (r//3)*3+c//3
>   Possibilities= 0
>   for d in range(1,10) :

On this condition (below) you can try to check which condition is True more
often and put that condition first in order to take advantage of short
circuiting and minimize array access.

> if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] :
continue
> Possibilities+= 1
>
>   if ( Possibilities < minPoss ) :
> minPoss= Possibilities
> Best= (r,c)
>
>   if minPoss == 0 : Best=(-1,-1)
>   return Best

Well I think that is as far as I go with my knowledge. Someone who knows
numpy arrays' performance traits should be able to help you more.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 10:38:35 +0100, Fábio Santos wrote:

> [Skipping to bottleneck]
> 
>> def find_good_cell() :
> 
> In this function you are accessing global variables a lot of times. Since
> accessing globals takes much more time than accessing locals, I advise you
> to assign them to local names, and use them.

I've tried to use local "references" like
G= Grid and using G instead of Grid below and similarly for
Row_Digits, Col_Digits and Sqr_Digits but it had no noticeable effect.

> 
>>   Best= None
>>   minPoss= 10
>>   for r in range(9) :
>> for c in range(9) :
>>   if  Grid[r,c] > 0 : continue
>>   Sq_No= (r//3)*3+c//3
>>   Possibilities= 0
>>   for d in range(1,10) :
> 
> On this condition (below) you can try to check which condition is True more
> often and put that condition first in order to take advantage of short
> circuiting and minimize array access.
Unfortunately, that's unpredictable.
> 
>> if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] :
> continue

Many thanks,
Helmut
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Oscar Benjamin
On 5 July 2013 09:22, Helmut Jarausch  wrote:
> Hi,
>
> I have coded a simple algorithm to solve a Sudoku (probably not the first 
> one).
> Unfortunately, it takes 13 seconds for a difficult problem which is more than 
> 75 times slower
> than the same algorithm coded in C++.
> Is this to be expected or could I have made my Python version faster *** 
> without *** sacrificing readability.

It is to be expected that this kind of processing is faster in C than
in straight Python. Where Python can win in these kind of problems is
if it makes it easier to implement a better algorithm but if your code
is just a transliteration from C to Python you should expect it to run
slower. Another way that Python can win is if you can express your
problem in terms of optimised operations from e.g. the numpy library
but you're not doing that here.

Of course that does depend on your Python implementation so you could
try e.g. using PyPy/nuitka/cython etc. to speed up the core
processing.

> Profiling shows that the function find_good_cell is called (only) 45267 times 
> and this take 12.9 seconds
> CPU time (on a 3.2 GHz machine)
[snip]
>
> def find_good_cell() :
>   Best= None
>   minPoss= 10
>   for r in range(9) :
> for c in range(9) :
>   if  Grid[r,c] > 0 : continue
>   Sq_No= (r//3)*3+c//3
>   Possibilities= 0
>   for d in range(1,10) :
> if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : 
> continue
> Possibilities+= 1
>
>   if ( Possibilities < minPoss ) :
> minPoss= Possibilities
> Best= (r,c)
>
>   if minPoss == 0 : Best=(-1,-1)
>   return Best

My one comment is that you're not really making the most out of numpy
arrays. Numpy's ndarrays are efficient when each line of Python code
is triggering a large number of numerical computations performed over
the array. Because of their N-dimensional nature and the fact that
they are in some sense second class citizens in CPython they are often
not as good as lists for this kind of looping and indexing.

I would actually expect this program to run faster with ordinary
Python lists and lists of lists. It means that you need to change e.g.
Grid[r, c] to Grid[r][c] but really I think that the indexing syntax
is all you're getting out of numpy here.


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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Benjamin Kaplan  writes:

>> FYI, your code above is (almost) exactly equivalent to the simpler
>
> They aren't equivalent. "except Exception" won't catch KeyboardInterrupt or
> SystemExit or a few others that you really don't want to catch in a generic
> error handler. You should almost never have a bare except.

I know, that's why I added "(almost)", I was just trying to explain why
he wasn't able to see the problem.

Thanks for pointing out the difference,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Νίκος Gr33k  writes:

> You can view the error in the very first line here:
>
> http://superhost.gr/?show=log&page=index.html

No, visiting that page simply emit the standard Apache error page,
without details.

>
> which yields: TypeError("'_Environ' object is not callable",)

Dave already told you the reason[1]. Νίκος, *read* **and** *understand*
our *whole* answers to your questions, otherwise we are wasting time,
you, and us!

ciao, lele.

[1] “… The other one is perhaps more subtle; I replaced square brackets
 with parentheses.”
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 12:25 μμ, ο/η Νίκος Gr33k έγραψε:


try:
 remadd = os.environ('REMOTE_ADDR')
 tuple3 = socket.gethostbyaddr(remadd)
 host = tuple3[0]
except Exception as e:
 host = repr(e)



which yields: TypeError("'_Environ' object is not callable",)


Any thoufgs as to why os.environ('REMOTE_ADDR') gives the above error?

I noticed that if i remove my domain from cloudflare the gethostbyaddr 
as it uses too months now.

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 1:24 μμ, ο/η Lele Gaifax έγραψε:

Νίκος Gr33k  writes:


You can view the error in the very first line here:

http://superhost.gr/?show=log&page=index.html


No, visiting that page simply emit the standard Apache error page,
without details.



which yields: TypeError("'_Environ' object is not callable",)


Dave already told you the reason[1]. Νίκος, *read* **and** *understand*
our *whole* answers to your questions, otherwise we are wasting time,
you, and us!

ciao, lele.

[1] “… The other one is perhaps more subtle; I replaced square brackets
  with parentheses.”



I read carefully all of tour answer please try to load again:

http://superhost.gr/?show=log&page=index.html

Di i miss an explanation on this?

TypeError("'_Environ' object is not callable",)

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 12:21 μμ, ο/η Dave Angel έγραψε:

Traceback (most recent call last):
   File "", line 1, in 
   File "/usr/local/lib/python3.3/os.py", line 669, in __getitem__
 value = self._data[self.encodekey(key)]
KeyError: b'REMOTE_ADDR



Wait!
Are you saying that the ip address is being returned as a byte string 
which then i have to decode with something like:


host = socket.gethostbyaddr( os.environ['REMOTE_HOST'].decode('utf-8') )[0]

?

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Νίκος Gr33k  writes:

> Στις 5/7/2013 12:25 μμ, ο/η Νίκος Gr33k έγραψε:
>>
>> try:
>>  remadd = os.environ('REMOTE_ADDR')
>>  tuple3 = socket.gethostbyaddr(remadd)
>>  host = tuple3[0]
>> except Exception as e:
>>  host = repr(e)
>
>> which yields: TypeError("'_Environ' object is not callable",)
>
> Any thoufgs as to why os.environ('REMOTE_ADDR') gives the above error?

Yes, I'd try to understand the error message, and eventually lookup the
documentation on os.environ to see what type of object it is bound
to. The solution should be easy.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 1:36 μμ, ο/η Lele Gaifax έγραψε:

Νίκος Gr33k  writes:


Στις 5/7/2013 12:25 μμ, ο/η Νίκος Gr33k έγραψε:


try:
  remadd = os.environ('REMOTE_ADDR')
  tuple3 = socket.gethostbyaddr(remadd)
  host = tuple3[0]
except Exception as e:
  host = repr(e)



which yields: TypeError("'_Environ' object is not callable",)


Any thoufgs as to why os.environ('REMOTE_ADDR') gives the above error?


Yes, I'd try to understand the error message, and eventually lookup the
documentation on os.environ to see what type of object it is bound
to. The solution should be easy.


Looks now when i print( repr(e)) i get

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3

but what string does it try to decode and jeeps failing?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 11:13:33 +0100, Oscar Benjamin wrote:

> My one comment is that you're not really making the most out of numpy
> arrays. Numpy's ndarrays are efficient when each line of Python code
> is triggering a large number of numerical computations performed over
> the array. Because of their N-dimensional nature and the fact that
> they are in some sense second class citizens in CPython they are often
> not as good as lists for this kind of looping and indexing.
> 
> I would actually expect this program to run faster with ordinary
> Python lists and lists of lists. It means that you need to change e.g.
> Grid[r, c] to Grid[r][c] but really I think that the indexing syntax
> is all you're getting out of numpy here.
> 

Thanks Oscar, that was a big improvement, indeed.
Using lists of lists instead of numpy arrays made the code more than
twice as fast (13 seconds down to 6 seconds)

Since I don't do any numerical stuff with the arrays, Numpy doesn't seem to be
a good choice. I think this is an argument to add real arrays to Python.

I even tried to use dictionaries instead of Numpy arrays. This version is a bit
slower then the lists of lists version (7.2 seconds instead of 6 second) but 
still
much faster than the Numpy array solution.

Helmut

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Νίκος Gr33k  writes:

> Looks now when i print( repr(e)) i get
>
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3
>
> but what string does it try to decode and jeeps failing?

Reasonably it's the second one, as the first clearly seems the tag of
the decoder that tried to translate it to Unicode.

As already explained, your immediate goal should be trying to understand
from *where* that byte string is coming. I can't help on that, sorry.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 1:59 μμ, ο/η Lele Gaifax έγραψε:

Νίκος Gr33k  writes:


Looks now when i print( repr(e)) i get

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3

but what string does it try to decode and jeeps failing?


Reasonably it's the second one, as the first clearly seems the tag of
the decoder that tried to translate it to Unicode.


2nd one of what? 2nd byte in order?
Can ou show me form which characters does this string consist of so we 
migth have an idea of where its coming from if we know what it looks like?



As already explained, your immediate goal should be trying to understand
from *where* that byte string is coming. I can't help on that, sorry.


Thats what i'm trying to do.
If i completely remove the gethostbyaddr fucntion adds function then 
there is nor problem.


The problem is recreating when the script tries to decode a hostname.

For some bizarre reason if i exclude my domain from CloudFlare then the 
reverse DNS resolution of the visitors hostname would be returned properly.


I will do it right now for you to see.
--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Νίκος Gr33k  writes:

> Στις 5/7/2013 1:59 μμ, ο/η Lele Gaifax έγραψε:
>> Νίκος Gr33k  writes:
>>
>>> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3
>>>
>>> but what string does it try to decode and jeeps failing?
>>
>> Reasonably it's the second one, as the first clearly seems the tag of
>> the decoder that tried to translate it to Unicode.
>
> 2nd one of what? 2nd byte in order?

You asked “what string” (although you probably meant “which string”):

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3

first string---^^^
second string---^^

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 2:05 μμ, ο/η Νίκος Gr33k έγραψε:

Thats what i'm trying to do.
If i completely remove the gethostbyaddr fucntion adds function then
there is nor problem.

The problem is recreating when the script tries to decode a hostname.

For some bizarre reason if i exclude my domain from CloudFlare then the
reverse DNS resolution of the visitors hostname would be returned properly.

I will do it right now for you to see.



Precisely as i have said it would happen:

I removed the domain form CloudFlare and your domains visting my website 
appearing as usual.


Its only when i cloudflare it and the UnicodeError happens.

But why?!?! I see no reason as to why when my domain becomes Cloudflared 
the gethostbyaddr() fails.


What kind of weird strings does it return back?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 2:16 μμ, ο/η Lele Gaifax έγραψε:

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3

first string---^^^
second string---^^



Hold on please!

From where do these dashes and carets characters come from?
Also from where do you see 2 strings?

Looking at that: UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3

ic an understandn onlt that utf-8 has failsed decoding some byte stream 
satrting with \xb6


I totally not follow...
--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread feedthetroll
Am Freitag, 5. Juli 2013 12:33:05 UTC+2 schrieb Νίκος Gr33k:
> ...
> Wait!
> Are you saying that the ip address is being returned as a byte string 
> which then i have to decode with something like:
> 
> host = socket.gethostbyaddr( os.environ['REMOTE_HOST'].decode('utf-8') )[0]

Wait!
I get a decode error when python tries to automatically decode a bytestring 
assuming it to be utf-8 encoded.
I am sure the error will disappear, when I try to decode it explicit using 
utf-8. Heureka! I got it!

Or in other words:
If a big stone falls on my foot accidently it hurts.
But I am sure it will not hurt, if take that same stone and throw it on my foot.
Heureka! I got it!

P.S.:

Am 14.06.2013 10:35, schrieb Fábio Santos:
> Also you have been shown this link and I feel you really need to read it.
> http://slash7.com/2006/12/22/vampires/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Steven D'Aprano
On Fri, 05 Jul 2013 10:53:35 +, Helmut Jarausch wrote:

> Since I don't do any numerical stuff with the arrays, Numpy doesn't seem
> to be a good choice. I think this is an argument to add real arrays to
> Python.

Guido's time machine strikes again:

import array


By the way, I'm not exactly sure how you go from "I don't do numerical 
calculations on numpy arrays" to "therefore Python should have arrays".


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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread feedthetroll
Am Freitag, 5. Juli 2013 13:27:25 UTC+2 schrieb Νίκος Gr33k:
> Στις 5/7/2013 2:16 μμ, ο/η Lele Gaifax έγραψε:
>> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3
>> first string---^^^
>> second string---^^
> 
> Hold on please!
>  From where do these dashes and carets characters come from?

ROTFL
Nikos, you made my day, again! Fun is back in these threads!

> 
> Also from where do you see 2 strings?

Look, my little dumb baby: The dashes and carets point to the strings.
The first one being 'utf-8', the second one starting with '\xb6\xe3\xed' (being 
a bytestring, therefore the b before the ')
Sorry, I forgot. You are not using python for your business, therefore you 
can't know, that strings in python can for example be identified by surrounding 
'.

> ic an understandn onlt that utf-8 has failsed decoding some byte stream 
> satrting with \xb6
So ... where did you call utf-8() so that it could try to decode something?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Important features for editors

2013-07-05 Thread rusi
On Thursday, July 4, 2013 1:37:10 PM UTC+5:30, Göktuğ Kayaalp wrote:
> Programmability comes to my mind, before anything else.  I'd suggest
> to find out about designs of Emacs and Vi(m).

There's one reason I prefer emacs -- and I guess some people prefer Idle -- the 
interpreter and editor are tightly integrated.

This is not strictly in the class of editor-as-editor but editor as 
programming-in-the-tiny support.

That said it needs to be also remembered:
- needs some setup efforts for python
- key-bindings and terminology will seem weird to the younger generation

One expansion for EMACS is Editor for Middle Aged Computer Scientists -- so I 
am guessing if you're asking the question you dont qualify :-)

If you get past these initial hurdles and also the initial programming hurdles 
to get to the point where you have the AHA moment -- programming is like 
art/poetry, you may want to look at emacs -> org-mode -> babel
which is the state-of-art system for what is called 'literate programming'
http://orgmode.org/worg/org-contrib/babel/intro.html

[I am a medium-grade user of org-mode and completely noob at babel! ]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread feedthetroll
Am Freitag, 5. Juli 2013 12:33:05 UTC+2 schrieb Νίκος Gr33k:
> ...
> Wait!
> Are you saying that the ip address is being returned as a byte string 
> which then i have to decode with something like:
> 
> host = socket.gethostbyaddr( os.environ['REMOTE_HOST'].decode('utf-8') )[0]

Wait!
I get a decode error when python tries to automatically decode a bytestring
assuming it to be utf-8 encoded.
I am sure the error will disappear, when I try to decode it explicit using
utf-8. Heureka! I got it!

Or in other words:
If a big stone falls on my foot accidently, it hurts.
--^
But I am sure it will not hurt, if take that same stone and throw it on my foot.

Heureka! I got it!



P.S.:

Am 14.06.2013 10:35, schrieb Fábio Santos:
> Also you have been shown this link and I feel you really need to read it.
> http://slash7.com/2006/12/22/vampires/
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k




 Αρχικό μήνυμα 
Θέμα: 	Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in 
position 0: invalid start byte

Ημερομηνία: Fri, 05 Jul 2013 15:16:45 +0300
Από:Νίκος Gr33k 
Προς:   feedthetr...@gmx.de



Στις 5/7/2013 3:06 μμ, ο/η feedthetr...@gmx.de έγραψε:

Am Freitag, 5. Juli 2013 13:27:25 UTC+2 schrieb Νίκος Gr33k:

Στις 5/7/2013 2:16 μμ, ο/η Lele Gaifax έγραψε:

UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3
first string---^^^
second string---^^


Hold on please!
  From where do these dashes and carets characters come from?


ROTFL
Nikos, you made my day, again! Fun is back in these threads!


Oh my God! i though they were the actual sting not that the pointed to
the string themselves! I ebn laughted at me!

Also from where do you see 2 strings?


Look, my little dumb baby: The dashes and carets point to the strings.
The first one being 'utf-8', the second one starting with '\xb6\xe3\xed' (being 
a bytestring, therefore the b before the ')
Sorry, I forgot. You are not using python for your business, therefore you 
can't know, that strings in python can for example be identified by surrounding 
'.



Indeed, but where is the 2nd sttring coming from?

ic an understandn onlt that utf-8 has failsed decoding some byte stream
satrting with \xb6

So ... where did you call utf-8() so that it could try to decode something?


Thats still unknown,

At least i manages to solve this by:

try:
  host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
except Exception as e:
  host = repr(e)


Seems like when you cloudflare a domain you can  no longer have the
originates ip address of the visitor but you have to read the above
environmental variable to be bale to retrieve it!



--
What is now proved was at first only imagined!




--
What is now proved was at first only imagined!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Fábio Santos
On 5 Jul 2013 11:58, "Helmut Jarausch"  wrote:
>
> On Fri, 05 Jul 2013 11:13:33 +0100, Oscar Benjamin wrote:
>
> > My one comment is that you're not really making the most out of numpy
> > arrays. Numpy's ndarrays are efficient when each line of Python code
> > is triggering a large number of numerical computations performed over
> > the array. Because of their N-dimensional nature and the fact that
> > they are in some sense second class citizens in CPython they are often
> > not as good as lists for this kind of looping and indexing.
> >
> > I would actually expect this program to run faster with ordinary
> > Python lists and lists of lists. It means that you need to change e.g.
> > Grid[r, c] to Grid[r][c] but really I think that the indexing syntax
> > is all you're getting out of numpy here.
> >
>
> Thanks Oscar, that was a big improvement, indeed.
> Using lists of lists instead of numpy arrays made the code more than
> twice as fast (13 seconds down to 6 seconds)
>
> Since I don't do any numerical stuff with the arrays, Numpy doesn't seem
to be
> a good choice. I think this is an argument to add real arrays to Python.
>
> I even tried to use dictionaries instead of Numpy arrays. This version is
a bit
> slower then the lists of lists version (7.2 seconds instead of 6 second)
but still
> much faster than the Numpy array solution.

May I suggest you avoid range and use enumerate(the_array) instead? It
might be faster.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Dave Angel

On 07/05/2013 06:33 AM, Νίκος Gr33k wrote:

Στις 5/7/2013 12:21 μμ, ο/η Dave Angel έγραψε:

Traceback (most recent call last):
   File "", line 1, in 
   File "/usr/local/lib/python3.3/os.py", line 669, in __getitem__
 value = self._data[self.encodekey(key)]
KeyError: b'REMOTE_ADDR



Wait!
Are you saying that the ip address is being returned as a byte string
which then i have to decode with something like:

host = socket.gethostbyaddr( os.environ['REMOTE_HOST'].decode('utf-8') )[0]



Don't fix the problem till you understand it.  Figure out who is dealing 
with a byte string here, and where that byte string came from.  Adding a 
decode, especially one that's going to do the same decode as your 
original error message, is very premature.


You're quoting from my error output, and that's caused because I don't 
have such an environment variable.  But you do.  So why aren't you in 
there debugging it?  And why on earth are you using the complex 
expression instead of a refactored one which might be simple enough for 
you to figure out what's wrong with it.


There is definitely something strange going on with that os.environ 
reference (NOT call). So have you yet succeeded in running the factored 
lines?  If you can't get them to run, at least up to the point that you 
get that unicode error, then you'll make progress only by guessing.


Get to that interactive debug session, and enter the lines till you get 
an error.  Then at least you know which line is causing the error.


   xxx = os.environ['REMOTE_HOST']
   yyy = socket.gethostbyaddr(xxx)
   host = yyy[0]


I'll bet the real problem is you're using some greek characters in the 
name of the environment variable, rather than "REMOTE_HOST"  So 
everything you show us is laboriously retyped, hiding the real problems 
underneath.



--
DaveA

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


Re: Default scope of variables

2013-07-05 Thread Neil Cerutti
On 2013-07-04, Dave Angel  wrote:
> On 07/04/2013 01:32 AM, Steven D'Aprano wrote:
>
>> Well, if I ever have more than 63,000,000 variables[1] in a
>> function, I'll keep that in mind.
>>
>  
>>
>> [1] Based on empirical evidence that Python supports names
>> with length at least up to one million characters long, and
>> assuming that each character can be an ASCII letter, digit or
>> underscore.
>
> Well, the number wouldn't be 63,000,000.  Rather it'd be
> 63**100

You should really count only the ones somebody might actually
want to use. That's a much, much smaller number, though still
plenty big.

Inner scopes (I don't remember the official name) is a great
feature of C++. It's not the right feature for Python, though,
since Python doesn't have deterministic destruction. It wouldn't
buy much except for namespace tidyness.

for x in range(4):
   print(x)
print(x) # Vader NOoOO!!!

Python provides deterministic destruction with a different
feature.

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


Re: How to make this faster

2013-07-05 Thread Oscar Benjamin
On 5 July 2013 11:53, Helmut Jarausch  wrote:
> I even tried to use dictionaries instead of Numpy arrays. This version is a 
> bit
> slower then the lists of lists version (7.2 seconds instead of 6 second) but 
> still
> much faster than the Numpy array solution.

When you switched to dictionaries did you take advantage of the
sparseness by iterating over dictionary keys instead of indices? This
is the kind of thing that I meant when I said that in Python it's
often easier to implement a better algorithm than in C. What I mean is
that if Grid is a dict so that Grid[(r, c)] is the entry at row r and
column c (if it exists) then you can change a loop like:

for r in range(9):
for c in range(9):
if Grid[r, c] > 0: continue
# do stuff

so that it looks like:

for r, c in Grid:
# do stuff

If the grid is sparsely occupied then this could be a significant improvement.


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


Re: Default scope of variables

2013-07-05 Thread Chris Angelico
On Fri, Jul 5, 2013 at 11:24 PM, Neil Cerutti  wrote:
> Python provides deterministic destruction with a different
> feature.

You mean 'with'? That's not actually destruction, it just does one of
the same jobs that deterministic destruction is used for (RAII). It
doesn't, for instance, have any influence on memory usage, nor does it
ensure the destruction of the object's referents. But yes, it does
achieve (one of) the most important role(s) of destruction.

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


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Lele Gaifax
Dave Angel  writes:

> You're quoting from my error output, and that's caused because I don't
> have such an environment variable.  But you do.

Dave, maybe you already know, but that variable is "injected" by the CGI
mechanism, is not coming from the OP shell environment.

As Νίκος discovered, when he "cloudfare" (whatever that means) his site,
the REMOTE_HOST envvar contains some (I guess) latin-greek encoded
string, and the remote address is carried by a different envvar...

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: Important features for editors

2013-07-05 Thread Grant Edwards
On 2013-07-04, Ferrous Cranus  wrote:
>  4/7/2013 9:40 , ??/?? Grant Edwards :
>> On 2013-07-04, ??  wrote:
>>>
>>> If you guys want to use it i can send you a patch for it. I know its
>>> illegal thing to say but it will help you use it without buying it.
>>
>> A new low.  Now he's offering to help people steal others' work.
>
> Like you never downloaded serials/keygens/patch/cracks for warez and 
> torrents websites.

No, not that I can recall.

-- 
Grant Edwards   grant.b.edwardsYow! In Newark the
  at   laundromats are open 24
  gmail.comhours a day!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 14:41:23 +0100, Oscar Benjamin wrote:

> On 5 July 2013 11:53, Helmut Jarausch  wrote:
>> I even tried to use dictionaries instead of Numpy arrays. This version is a 
>> bit
>> slower then the lists of lists version (7.2 seconds instead of 6 second) but 
>> still
>> much faster than the Numpy array solution.
> 
> When you switched to dictionaries did you take advantage of the
> sparseness by iterating over dictionary keys instead of indices? This
> is the kind of thing that I meant when I said that in Python it's
> often easier to implement a better algorithm than in C. What I mean is
> that if Grid is a dict so that Grid[(r, c)] is the entry at row r and
> column c (if it exists) then you can change a loop like:
> 
> for r in range(9):
> for c in range(9):
> if Grid[r, c] > 0: continue
> # do stuff
> 
> so that it looks like:
> 
> for r, c in Grid:
> # do stuff
> 
> If the grid is sparsely occupied then this could be a significant improvement.
> 
> 
> Oscar

This gives a big speedup. Now, the time is gone down to 1.73 seconds in 
comparison to
original 13 seconds or the 7 seconds for the first version above.

Many thanks,
it seems hard to optimize a Python program,
Helmut
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 5:11 μμ, ο/η Lele Gaifax έγραψε:

Dave Angel  writes:


You're quoting from my error output, and that's caused because I don't
have such an environment variable.  But you do.


Dave, maybe you already know, but that variable is "injected" by the CGI
mechanism, is not coming from the OP shell environment.

As Νίκος discovered, when he "cloudfare" (whatever that means) his site,
the REMOTE_HOST envvar contains some (I guess) latin-greek encoded
string, and the remote address is carried by a different envvar...


Exactly only when i CloudFlare(www.cloudflare.com) the domain the 
hostname cannot be retrieved.


At least i managed to solve this by:

try:
  host = socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
except Exception as e:
  host = repr(e)


Seems like when you cloudflare a domain you can  no longer have the
originates ip address of the visitor but you have to read the above
environmental variable to be bale to retrieve it!

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Oscar Benjamin
On 5 July 2013 15:28, Helmut Jarausch  wrote:
> On Fri, 05 Jul 2013 14:41:23 +0100, Oscar Benjamin wrote:
>
>> On 5 July 2013 11:53, Helmut Jarausch  wrote:
>>> I even tried to use dictionaries instead of Numpy arrays. This version is a 
>>> bit
>>> slower then the lists of lists version (7.2 seconds instead of 6 second) 
>>> but still
>>> much faster than the Numpy array solution.
>>
>> When you switched to dictionaries did you take advantage of the
>> sparseness by iterating over dictionary keys instead of indices? This
>> is the kind of thing that I meant when I said that in Python it's
>> often easier to implement a better algorithm than in C. What I mean is
>> that if Grid is a dict so that Grid[(r, c)] is the entry at row r and
>> column c (if it exists) then you can change a loop like:
>>
>> for r in range(9):
>> for c in range(9):
>> if Grid[r, c] > 0: continue
>> # do stuff
>>
>> so that it looks like:
>>
>> for r, c in Grid:
>> # do stuff
>>
>> If the grid is sparsely occupied then this could be a significant 
>> improvement.
>>
> This gives a big speedup. Now, the time is gone down to 1.73 seconds in 
> comparison to
> original 13 seconds or the 7 seconds for the first version above.

Presumably then you're now down to the innermost loop as a bottle-neck:

  Possibilities= 0
  for d in range(1,10) :
if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
Possibilities+= 1

If you make it so that e.g. Row_Digits[r] is a set of indices rather
than a list of bools then you can do this with something like

Possibilities = len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])

or perhaps

Possibilities = len(set.union(Row_Digits[r], Col_Digits[c],
Sqr_Digits[Sq_No]))

which I would expect to be a little faster than looping over range
since the loop is then performed under the hood by the builtin
set-type.

> Many thanks,
> it seems hard to optimize a Python program,

It just takes practice. It's a little less obvious in Python than in
low-level languages where the bottlenecks will be and which operations
are faster/slower but optimisation always involves a certain amount of
trial and error anyway.


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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 12:02:21 +, Steven D'Aprano wrote:

> On Fri, 05 Jul 2013 10:53:35 +, Helmut Jarausch wrote:
> 
>> Since I don't do any numerical stuff with the arrays, Numpy doesn't seem
>> to be a good choice. I think this is an argument to add real arrays to
>> Python.
> 
> Guido's time machine strikes again:
> 
> import array
> 
> 
> By the way, I'm not exactly sure how you go from "I don't do numerical 
> calculations on numpy arrays" to "therefore Python should have arrays".

I should have been more clear. I meant multi-dimensional arrays (2D, at least)
Numpy is fine if I do math with matrices (without loops in python).

Given that I don't like to use the old FORTRAN way (when "dynamic" arrays are 
passed to
functions) of indexing a 2-d array I would need a MACRO or an INLINED function 
in Python
or something like a META-compiler phase transforming

def access2d(v,i,j,dim1) :  # doesn't work on the l.h.s.
  return v[i*dim1+j]

access2d(v,i,j,dim1) = 7# at compile time, please

to

v[i*dim1+j]= 7  # this, by itself, is considered ugly (the FORTRAN way)

Thanks for the discussion,
Helmut




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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 13:44:57 +0100, Fábio Santos wrote:
May I suggest you avoid range and use enumerate(the_array) instead? It
might be faster.

How does this work?

Given

Grid= [[0 for j in range(9)] for i in range(9)]

for (r,c,val) in (Grid) :

Helmut

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


Re: How to make this faster

2013-07-05 Thread Fábio Santos
On 5 Jul 2013 15:59, "Helmut Jarausch"  wrote:
>
> On Fri, 05 Jul 2013 13:44:57 +0100, Fábio Santos wrote:
> May I suggest you avoid range and use enumerate(the_array) instead? It
> might be faster.
>
> How does this work?
>
> Given
>
> Grid= [[0 for j in range(9)] for i in range(9)]
>
> for (r,c,val) in (Grid) :
>
> Helmut

for r, row_lst in enumerate(Grid):
for c, val in enumerate(row_lst):
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 15:45:25 +0100, Oscar Benjamin wrote:

> Presumably then you're now down to the innermost loop as a bottle-neck:
> 
>   Possibilities= 0
>   for d in range(1,10) :
> if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : 
> continue
> Possibilities+= 1
> 
> If you make it so that e.g. Row_Digits[r] is a set of indices rather
> than a list of bools then you can do this with something like
> 
> Possibilities = len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
> 
> or perhaps
> 
> Possibilities = len(set.union(Row_Digits[r], Col_Digits[c],
> Sqr_Digits[Sq_No]))
> 
> which I would expect to be a little faster than looping over range
> since the loop is then performed under the hood by the builtin
> set-type.
> 
> It just takes practice. 

indeed

> It's a little less obvious in Python than in
> low-level languages where the bottlenecks will be and which operations
> are faster/slower but optimisation always involves a certain amount of
> trial and error anyway.
> 
> 
> Oscar

I've tried the following version

def find_good_cell() :
  Best= None
  minPoss= 10
  for r,c in Grid :
if  Grid[(r,c)] > 0 : continue
Sq_No= (r//3)*3+c//3
Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
if ( Possibilities < minPoss ) :
  minPoss= Possibilities
  Best= (r,c)

  if minPoss == 0 : Best=(-1,-1)
  return Best

All_digits= set((1,2,3,4,5,6,7,8,9))

def Solve(R_Cells) :
  if  R_Cells == 0 :
print("\n\n++ S o l u t i o n ++\n")
Print_Grid()
return True

  r,c= find_good_cell()
  if r < 0 : return False
  Sq_No= (r//3)*3+c//3
  
  for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
# put d into Grid
Grid[(r,c)]= d
Row_Digits[r].add(d)
Col_Digits[c].add(d)
Sqr_Digits[Sq_No].add(d)

Success= Solve(R_Cells-1)

# remove d again
Grid[(r,c)]= 0
Row_Digits[r].remove(d)
Col_Digits[c].remove(d)
Sqr_Digits[Sq_No].remove(d)
  
if Success :
  Zuege.append((d,r,c))
  return True

  return False

which turns out to be as fast as the previous "dictionary only version".
Probably,  set.remove is a bit slow

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


Re: How to make this faster

2013-07-05 Thread Oscar Benjamin
On 5 July 2013 15:48, Helmut Jarausch  wrote:
> On Fri, 05 Jul 2013 12:02:21 +, Steven D'Aprano wrote:
>
>> On Fri, 05 Jul 2013 10:53:35 +, Helmut Jarausch wrote:
>>
>>> Since I don't do any numerical stuff with the arrays, Numpy doesn't seem
>>> to be a good choice. I think this is an argument to add real arrays to
>>> Python.
>>
>> Guido's time machine strikes again:
>>
>> import array
>>
>>
>> By the way, I'm not exactly sure how you go from "I don't do numerical
>> calculations on numpy arrays" to "therefore Python should have arrays".
>
> I should have been more clear. I meant multi-dimensional arrays (2D, at least)
> Numpy is fine if I do math with matrices (without loops in python).
>
> Given that I don't like to use the old FORTRAN way (when "dynamic" arrays are 
> passed to
> functions) of indexing a 2-d array I would need a MACRO or an INLINED 
> function in Python
> or something like a META-compiler phase transforming
>
> def access2d(v,i,j,dim1) :  # doesn't work on the l.h.s.
>   return v[i*dim1+j]
>
> access2d(v,i,j,dim1) = 7# at compile time, please
>
> to
>
> v[i*dim1+j]= 7  # this, by itself, is considered ugly (the FORTRAN way)

The list of lists approach works fine for what you're doing. I don't
think that a[r][c] is that much worse than a[r, c]. It's only when you
want to do something like a[:, c] that it breaks down. In any case,
your algorithm would work better with Python's set/dict/list types
than numpy arrays.

One of the reasons that it's faster to use lists than numpy arrays (as
you found out) is precisely because the N-dimensional array logic
complicates 1-dimensional processing. I've seen discussions in Cython
and numpy about lighter-weight 1-dimensional array types for this
reason.

The other reason that numpy arrays are slower for what you're doing is
that (just like the stdlib array type Steven referred to) they use
homogeneous types in a contiguous buffer and each element is not a
Python object in its own right until you access it with e.g. a[0].
That means that the numpy array has to create a new object every time
you index into it whereas the list can simply return a new reference
to an existing object. You can get the same effect with numpy arrays
by using dtype=object but I'd still expect it to be slower for this.


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


Re: Default scope of variables

2013-07-05 Thread Neil Cerutti
On 2013-07-05, Chris Angelico  wrote:
> On Fri, Jul 5, 2013 at 11:24 PM, Neil Cerutti
>  wrote:
>> Python provides deterministic destruction with a different
>> feature.
>
> You mean 'with'? That's not actually destruction, it just does
> one of the same jobs that deterministic destruction is used for
> (RAII). It doesn't, for instance, have any influence on memory
> usage, nor does it ensure the destruction of the object's
> referents. But yes, it does achieve (one of) the most important
> role(s) of destruction.

Yes, thanks. I meant the ability to grab and release a
resource deterministically.

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


Re: How to make this faster

2013-07-05 Thread Oscar Benjamin
On 5 July 2013 16:17, Helmut Jarausch  wrote:
>
> I've tried the following version
>
> def find_good_cell() :
>   Best= None
>   minPoss= 10
>   for r,c in Grid :
> if  Grid[(r,c)] > 0 : continue

Sorry, I think what I meant was that you should have a structure
called e.g. Remaining which is the set of all (r, c) pairs that you
want to loop over here. Then there's no need to check on each
iteration whether or not Grid[(r, c)] > 0. When I said "sparse" I
meant that you don't need to set keys in Grid unless you actually have
a value there so the test "Grid[(r, c)] > 0" would look like "(r, c)
in Grid". Remaining is the set of all (r, c) pairs not in Grid that
you update incrementally with .add() and .remove().

Then this

   for r,c in Grid :
 if  Grid[(r,c)] > 0 : continue

becomes

for r, c in Remaining:

> Sq_No= (r//3)*3+c//3
> Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
> if ( Possibilities < minPoss ) :
>   minPoss= Possibilities
>   Best= (r,c)
>
>   if minPoss == 0 : Best=(-1,-1)
>   return Best
>
> All_digits= set((1,2,3,4,5,6,7,8,9))

All_digits= set(range(1, 10))

or

All_digits = {1,2,3,4,5,6,7,8,9}

>
> def Solve(R_Cells) :
>   if  R_Cells == 0 :
> print("\n\n++ S o l u t i o n ++\n")
> Print_Grid()
> return True
>
>   r,c= find_good_cell()
>   if r < 0 : return False
>   Sq_No= (r//3)*3+c//3
>
>   for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
> # put d into Grid
> Grid[(r,c)]= d
> Row_Digits[r].add(d)
> Col_Digits[c].add(d)
> Sqr_Digits[Sq_No].add(d)
>
> Success= Solve(R_Cells-1)
>
> # remove d again
> Grid[(r,c)]= 0
> Row_Digits[r].remove(d)
> Col_Digits[c].remove(d)
> Sqr_Digits[Sq_No].remove(d)
>
> if Success :
>   Zuege.append((d,r,c))
>   return True
>
>   return False
>
> which turns out to be as fast as the previous "dictionary only version".
> Probably,  set.remove is a bit slow

No it's not and you're not using it in your innermost loops anyway.
Probably the loop I referred to isn't your bottleneck.


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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 16:18:41 +0100, Fábio Santos wrote:

> On 5 Jul 2013 15:59, "Helmut Jarausch"  wrote:
>>
>> On Fri, 05 Jul 2013 13:44:57 +0100, Fábio Santos wrote:
>> May I suggest you avoid range and use enumerate(the_array) instead? It
>> might be faster.
>>
>> How does this work?
>>
>> Given
>>
>> Grid= [[0 for j in range(9)] for i in range(9)]
>>
>> for (r,c,val) in (Grid) :
>>
>> Helmut
> 
> for r, row_lst in enumerate(Grid):
> for c, val in enumerate(row_lst):

This is only slightly faster. I assume the creation of the temporary lists 
"row_list"
is a bit expensive.
Taking 5.4 seconds it's much slower than the current champion ( 0.79 seconds )

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 16:38:43 +0100, Oscar Benjamin wrote:

> On 5 July 2013 16:17, Helmut Jarausch  wrote:
>>
>> I've tried the following version
>>
>> def find_good_cell() :
>>   Best= None
>>   minPoss= 10
>>   for r,c in Grid :
>> if  Grid[(r,c)] > 0 : continue
> 
> Sorry, I think what I meant was that you should have a structure
> called e.g. Remaining which is the set of all (r, c) pairs that you
> want to loop over here. Then there's no need to check on each
> iteration whether or not Grid[(r, c)] > 0. When I said "sparse" I
> meant that you don't need to set keys in Grid unless you actually have
> a value there so the test "Grid[(r, c)] > 0" would look like "(r, c)
> in Grid". Remaining is the set of all (r, c) pairs not in Grid that
> you update incrementally with .add() and .remove().
> 
> Then this
> 
>for r,c in Grid :
>  if  Grid[(r,c)] > 0 : continue
> 
> becomes
> 
> for r, c in Remaining:
> 
>> Sq_No= (r//3)*3+c//3
>> Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
>> if ( Possibilities < minPoss ) :
>>   minPoss= Possibilities
>>   Best= (r,c)
>>
>>   if minPoss == 0 : Best=(-1,-1)
>>   return Best
>>
>> All_digits= set((1,2,3,4,5,6,7,8,9))
> 
> All_digits= set(range(1, 10))
> 
> or
> 
> All_digits = {1,2,3,4,5,6,7,8,9}
> 
>>
>> def Solve(R_Cells) :
>>   if  R_Cells == 0 :
>> print("\n\n++ S o l u t i o n ++\n")
>> Print_Grid()
>> return True
>>
>>   r,c= find_good_cell()
>>   if r < 0 : return False
>>   Sq_No= (r//3)*3+c//3
>>
>>   for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
>> # put d into Grid
>> Grid[(r,c)]= d
>> Row_Digits[r].add(d)
>> Col_Digits[c].add(d)
>> Sqr_Digits[Sq_No].add(d)
>>
>> Success= Solve(R_Cells-1)
>>
>> # remove d again
>> Grid[(r,c)]= 0
>> Row_Digits[r].remove(d)
>> Col_Digits[c].remove(d)
>> Sqr_Digits[Sq_No].remove(d)
>>
>> if Success :
>>   Zuege.append((d,r,c))
>>   return True
>>
>>   return False
>>
>> which turns out to be as fast as the previous "dictionary only version".
>> Probably,  set.remove is a bit slow
> 
> No it's not and you're not using it in your innermost loops anyway.
> Probably the loop I referred to isn't your bottleneck.
> 

I've tried your suggestion, but unless I've misunderstood your suggestion it's 
a bit slower
than the solution above.

The solution above take 0.79 seconds (mean of 100 calls) while the following 
version
take 1.05 seconds (mean of 100 calls):


Grid = {(i,j):0 for i in range(9) for j in range(9)}
Remaining= {(i,j) for i in range(9) for j in range(9)}
Row_Digits = [set() for r in range(9)]
Col_Digits = [set() for c in range(9)]
Sqr_Digits = [set() for s in range(9)]

 remove some pairs from Remaining for the initial set of the given Sudoku

def find_good_cell() :
  Best= None
  minPoss= 10
  for r,c in Remaining :
Sq_No= (r//3)*3+c//3
Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
if ( Possibilities < minPoss ) :
  minPoss= Possibilities
  Best= (r,c)

  if minPoss == 0 : Best=(-1,-1)
  return Best

All_digits= set(range(1,10))

def Solve(R_Cells) :
  if  R_Cells == 0 :
print("\n\n++ S o l u t i o n ++\n")
Print_Grid()
return True

  r,c= find_good_cell()
  if r < 0 : return False
  Sq_No= (r//3)*3+c//3
  
  for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
# put d into Grid
Grid[(r,c)]= d
Remaining.remove((r,c))
Row_Digits[r].add(d)
Col_Digits[c].add(d)
Sqr_Digits[Sq_No].add(d)

Success= Solve(R_Cells-1)

# remove d again
Grid[(r,c)]= 0
Remaining.add((r,c))
Row_Digits[r].remove(d)
Col_Digits[c].remove(d)
Sqr_Digits[Sq_No].remove(d)
  
if Success :
  Zuege.append((d,r,c))
  return True

  return False


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


Re: How to make this faster

2013-07-05 Thread Steven D'Aprano
On Fri, 05 Jul 2013 14:54:26 +, Helmut Jarausch wrote:

> On Fri, 05 Jul 2013 13:44:57 +0100, Fábio Santos wrote: May I suggest
> you avoid range and use enumerate(the_array) instead? It might be
> faster.
> 
> How does this work?
> 
> Given
> 
> Grid= [[0 for j in range(9)] for i in range(9)]

This creates a list containing nine lists, each one of which contains 0 
repeated nine times.

That can be simplified to:

grid = [[0]*9 for i in range(9)]

which will likely be faster, although for such a small number of lists 
you may not notice the difference.


> for (r,c,val) in (Grid) :

This causes a SyntaxError, as  is not legal Python code. I'm not 
really sure what you think you are trying to do here. But I think Fábio 
means that instead of writing code like this:

for r in range(9):
for c in range(9):
value = grid[r][c]
...

you should consider writing this:

for r, row in enumerate(grid):
for c, value in enumerate(row):
...


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


Re: How to make this faster

2013-07-05 Thread MRAB

On 05/07/2013 16:17, Helmut Jarausch wrote:

On Fri, 05 Jul 2013 15:45:25 +0100, Oscar Benjamin wrote:


Presumably then you're now down to the innermost loop as a bottle-neck:

  Possibilities= 0
  for d in range(1,10) :
if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
Possibilities+= 1

If you make it so that e.g. Row_Digits[r] is a set of indices rather
than a list of bools then you can do this with something like

Possibilities = len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])

or perhaps

Possibilities = len(set.union(Row_Digits[r], Col_Digits[c],
Sqr_Digits[Sq_No]))

which I would expect to be a little faster than looping over range
since the loop is then performed under the hood by the builtin
set-type.

It just takes practice.


indeed


It's a little less obvious in Python than in
low-level languages where the bottlenecks will be and which operations
are faster/slower but optimisation always involves a certain amount of
trial and error anyway.


Oscar


I've tried the following version

def find_good_cell() :
   Best= None
   minPoss= 10
   for r,c in Grid :
 if  Grid[(r,c)] > 0 : continue
 Sq_No= (r//3)*3+c//3
 Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
 if ( Possibilities < minPoss ) :
   minPoss= Possibilities
   Best= (r,c)

   if minPoss == 0 : Best=(-1,-1)
   return Best

All_digits= set((1,2,3,4,5,6,7,8,9))

def Solve(R_Cells) :
   if  R_Cells == 0 :
 print("\n\n++ S o l u t i o n ++\n")
 Print_Grid()
 return True

   r,c= find_good_cell()
   if r < 0 : return False
   Sq_No= (r//3)*3+c//3

   for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
 # put d into Grid
 Grid[(r,c)]= d
 Row_Digits[r].add(d)
 Col_Digits[c].add(d)
 Sqr_Digits[Sq_No].add(d)

 Success= Solve(R_Cells-1)

 # remove d again
 Grid[(r,c)]= 0
 Row_Digits[r].remove(d)
 Col_Digits[c].remove(d)
 Sqr_Digits[Sq_No].remove(d)

 if Success :
   Zuege.append((d,r,c))
   return True

   return False

which turns out to be as fast as the previous "dictionary only version".
Probably,  set.remove is a bit slow


For comparison, here's my solution:

from collections import Counter

problem = '''
_
_3_85
__1_2
___5_7___
__4___1__
_9___
5__73
__2_1
4___9
'''

# Build the grid.
digits = "123456789"

grid = []

for row in problem.splitlines():
  if not row:
continue

  new_row = []

  for cell in row:
if cell.isdigit():
  new_row.append({cell})
else:
  new_row.append(set(digits))

  grid.append(new_row)

# Solve the grid.
changed = True
while changed:
  changed = False

  # Look for cells that contain only one digit.
  for r in range(9):
for c in range(9):
  if len(grid[r][c]) == 1:
digit = list(grid[r][c])[0]

# Remove from other cells in same row.
for c2 in range(9):
  if c2 != c and digit in grid[r][c2]:
grid[r][c2].remove(digit)
changed = True

# Remove from other cells in same column.
for r2 in range(9):
  if r2 != r and digit in grid[r2][c]:
grid[r2][c].remove(digit)
changed = True

# Remove from other cells in the same block of 9.
start_row = r - r % 3
start_column = c - c % 3
for r2 in range(start_row, start_row + 3):
  for c2 in range(start_column, start_column + 3):
if (r2, c2) != (r, c) and digit in grid[r2][c2]:
  grid[r2][c2].remove(digit)
  changed = True

  # Look for digits that occur in only one cell in a row.
  for r in range(9):
counts = Counter()
for c in range(9):
  counts += Counter(grid[r][c])

unique = {digit for digit, times in counts.items() if times == 1}

for c in range(9):
  if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
grid[r][c] &= unique
changed = True

  # Look for digits that occur in only one cell in a column.
  for c in range(9):
counts = Counter()
for r in range(9):
  counts += Counter(grid[r][c])

unique = {digit for digit, times in counts.items() if times == 1}

for r in range(9):
  if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
grid[r][c] &= unique
changed = True

  # Look for digits that occur in only one cell in a block of 9.
  for start_row in range(0, 9, 3):
for start_column in range(0, 9, 3):
  counts = Counter()
  for r in range(start_row, start_row + 3):
for c in range(start_column, start_column + 3):
  counts += Counter(grid[r][c])

  unique = {digit for digit, times in counts.items() if times == 1}

  for r in range(start_row, start_row + 3):
for c in range(start_column, start_column + 3):
  if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
grid[r][c] &= unique

Re: How to make this faster

2013-07-05 Thread Steven D'Aprano
On Fri, 05 Jul 2013 16:07:03 +, Helmut Jarausch wrote:

> The solution above take 0.79 seconds (mean of 100 calls) while the
> following version take 1.05 seconds (mean of 100 calls):

1) How are you timing the calls?

2) Don't use the mean, that's the wrong statistic when you are measuring 
something where the error is always one sided. You want the minimum, not 
the mean.

When you measure the time taken for a piece of code, the number you get 
is made up of two components:


1) the actual time the code would have taken, if there were no random 
fluctuations due to other processes, etc.; and

2) random errors due to switching to other processes, etc.

Both of these are unknown; you only know the total. But obviously the 
random errors are always positive. They can never be negative, and you 
can never measure a time which is less than the fastest your code could 
run.

(If your anti-virus software starts scanning in the middle of the trial, 
it can only make your code take more time to run, never less.)

So the only way to minimize the error is to pick the minimum time, not 
the average. The average just gives you:

  - some unknown "true" time, plus some unknown error, somewhere
between the smallest error and the biggest error;

whereas the minimum gives you:

  - some unknown "true" time, plus the smallest error yet seen.




-- 
Steven

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


Re: How to make this faster

2013-07-05 Thread Steven D'Aprano
On Fri, 05 Jul 2013 15:47:45 +, Helmut Jarausch wrote:

> > for r, row_lst in enumerate(Grid):
> > for c, val in enumerate(row_lst):
>
> I assume the creation of the temporary lists "row_list" is a bit
> expensive.

No temporary list is being created. The pre-existing list is just being 
grabbed, which is fast.



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


How to check for threads being finished?

2013-07-05 Thread Steven D'Aprano
I have a pool of worker threads, created like this:

threads = [MyThread(*args) for i in range(numthreads)]
for t in threads:
t.start()


I then block until the threads are all done:

while any(t.isAlive() for t in threads):
pass


Is that the right way to wait for the threads to be done? Should I stick 
a call to time.sleep() inside the while loop? If so, how long should I 
sleep? That's probably an unanswerable question, but some guidelines on 
choosing the sleep time will be appreciated.




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


Re: How to check for threads being finished?

2013-07-05 Thread Chris Angelico
On Sat, Jul 6, 2013 at 2:59 AM, Steven D'Aprano
 wrote:
> I then block until the threads are all done:
>
> while any(t.isAlive() for t in threads):
> pass
>

Using the threading module, I assume. Is there any reason you can't
simply join() each thread in succession?

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


Re: How to check for threads being finished?

2013-07-05 Thread Irmen de Jong
On 5-7-2013 18:59, Steven D'Aprano wrote:
> I then block until the threads are all done:
> 
> while any(t.isAlive() for t in threads):
> pass
> 
> 
> Is that the right way to wait for the threads to be done? Should I stick 
> a call to time.sleep() inside the while loop? If so, how long should I 
> sleep? That's probably an unanswerable question, but some guidelines on 
> choosing the sleep time will be appreciated.
> 

I think your while loop busy-waits until the threads are completed.
Do this instead:

for t in threads: t.join()# optionally pass a timeout to join


-Irmen

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


Re: How to check for threads being finished?

2013-07-05 Thread Ian Kelly
On Fri, Jul 5, 2013 at 10:59 AM, Steven D'Aprano
 wrote:
> I have a pool of worker threads, created like this:
>
> threads = [MyThread(*args) for i in range(numthreads)]
> for t in threads:
> t.start()
>
>
> I then block until the threads are all done:
>
> while any(t.isAlive() for t in threads):
> pass
>
>
> Is that the right way to wait for the threads to be done? Should I stick
> a call to time.sleep() inside the while loop? If so, how long should I
> sleep? That's probably an unanswerable question, but some guidelines on
> choosing the sleep time will be appreciated.

for thread in threads:
thread.join()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 16:50:41 +, Steven D'Aprano wrote:

> On Fri, 05 Jul 2013 16:07:03 +, Helmut Jarausch wrote:
> 
>> The solution above take 0.79 seconds (mean of 100 calls) while the
>> following version take 1.05 seconds (mean of 100 calls):
> 
> 1) How are you timing the calls?

I've put the work horse "Solve" into a loop executing 100 times.
That's on an otherwise idle Linux machine.

> 
> 2) Don't use the mean, that's the wrong statistic when you are measuring 
> something where the error is always one sided. You want the minimum, not 
> the mean.

Here you assume time measuring itself is without error - I doubt that.
If the timing version, which executes function "Solve" one hundred times,
runs about 80-100 seconds without a significant variation, then taking the
mean is mathematically correct. I can't take the minimum since I don't
measure the time a single call takes.

> 
> When you measure the time taken for a piece of code, the number you get 
> is made up of two components:
> 
> 
> 1) the actual time the code would have taken, if there were no random 
> fluctuations due to other processes, etc.; and
> 
> 2) random errors due to switching to other processes, etc.
> 
> Both of these are unknown; you only know the total. But obviously the 
> random errors are always positive. They can never be negative, and you 
> can never measure a time which is less than the fastest your code could 
> run.
> 
> (If your anti-virus software starts scanning in the middle of the trial, 
> it can only make your code take more time to run, never less.)
> 
> So the only way to minimize the error is to pick the minimum time, not 
> the average. The average just gives you:
> 
>   - some unknown "true" time, plus some unknown error, somewhere
> between the smallest error and the biggest error;
> 
> whereas the minimum gives you:
> 
>   - some unknown "true" time, plus the smallest error yet seen.

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 17:25:54 +0100, MRAB wrote:


> For comparison, here's my solution:

Your solution is very fast, indeed.
It takes 0.04 seconds (mean of 1000 runs) restoring "grid"
in between.
But that's a different algorithm which is IMHO more difficult to understand.
Many thanks,
Helmut

> 
> from collections import Counter
> 
> problem = '''
> _
> _3_85
> __1_2
> ___5_7___
> __4___1__
> _9___
> 5__73
> __2_1
> 4___9
> '''
> 
> # Build the grid.
> digits = "123456789"
> 
> grid = []
> 
> for row in problem.splitlines():
>if not row:
>  continue
> 
>new_row = []
> 
>for cell in row:
>  if cell.isdigit():
>new_row.append({cell})
>  else:
>new_row.append(set(digits))
> 
>grid.append(new_row)
> 
> # Solve the grid.
> changed = True
> while changed:
>changed = False
> 
># Look for cells that contain only one digit.
>for r in range(9):
>  for c in range(9):
>if len(grid[r][c]) == 1:
>  digit = list(grid[r][c])[0]
> 
>  # Remove from other cells in same row.
>  for c2 in range(9):
>if c2 != c and digit in grid[r][c2]:
>  grid[r][c2].remove(digit)
>  changed = True
> 
>  # Remove from other cells in same column.
>  for r2 in range(9):
>if r2 != r and digit in grid[r2][c]:
>  grid[r2][c].remove(digit)
>  changed = True
> 
>  # Remove from other cells in the same block of 9.
>  start_row = r - r % 3
>  start_column = c - c % 3
>  for r2 in range(start_row, start_row + 3):
>for c2 in range(start_column, start_column + 3):
>  if (r2, c2) != (r, c) and digit in grid[r2][c2]:
>grid[r2][c2].remove(digit)
>changed = True
> 
># Look for digits that occur in only one cell in a row.
>for r in range(9):
>  counts = Counter()
>  for c in range(9):
>counts += Counter(grid[r][c])
> 
>  unique = {digit for digit, times in counts.items() if times == 1}
> 
>  for c in range(9):
>if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
>  grid[r][c] &= unique
>  changed = True
> 
># Look for digits that occur in only one cell in a column.
>for c in range(9):
>  counts = Counter()
>  for r in range(9):
>counts += Counter(grid[r][c])
> 
>  unique = {digit for digit, times in counts.items() if times == 1}
> 
>  for r in range(9):
>if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
>  grid[r][c] &= unique
>  changed = True
> 
># Look for digits that occur in only one cell in a block of 9.
>for start_row in range(0, 9, 3):
>  for start_column in range(0, 9, 3):
>counts = Counter()
>for r in range(start_row, start_row + 3):
>  for c in range(start_column, start_column + 3):
>counts += Counter(grid[r][c])
> 
>unique = {digit for digit, times in counts.items() if times == 1}
> 
>for r in range(start_row, start_row + 3):
>  for c in range(start_column, start_column + 3):
>if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
>  grid[r][c] &= unique
>  changed = True
> 
> # Display the solution.
> for row in grid:
>for cell in row:
>  print("".join(sorted(cell)), end=" ")
> 
>print()

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


Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k
Is there a way to extract out of some environmental variable the Geo 
location of the user being the city the user visits out website from?


Perhaps by utilizing his originated ip address?

--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


analyzing time

2013-07-05 Thread noydb
Hello All,

I have a table with a column of type date, with dates and time combined (like 
'1/6/2013 3:52:69PM'), that spans many months.  How would I pull out records 
that are the first and last entries per day?

Also, if I wanted to find time clusters per day (or per week) -- like if an 
entry is made every day around 11am -- is there a way to get at that temporal 
statistical cluster?

Python 2.7, Windows 7.

Any guidance would be greatly appreciated!  Time seems tricky...

Thanks,

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


Re: analyzing time

2013-07-05 Thread Neil Cerutti
On 2013-07-05, noydb  wrote:
> Hello All,
>
> I have a table with a column of type date, with dates and time
> combined (like '1/6/2013 3:52:69PM'), that spans many months.
> How would I pull out records that are the first and last
> entries per day?
>
> Also, if I wanted to find time clusters per day (or per week)
> -- like if an entry is made every day around 11am -- is there a
> way to get at that temporal statistical cluster?
>
> Python 2.7, Windows 7.
>
> Any guidance would be greatly appreciated!  Time seems tricky...

Time *is* really tricky, but that's because we humans created a
tricky system. If can ignore issues of timespampts, timezones and
daylight savings time, then time handling in Python can be
simple.

datetime.datetime.strptime can translate the time format above
into datetime.datetime objects, which provide all the methods you
will need.

To find clusters and min and max values you will likely need to
put the datetime objects in a list, and use some Python builtins
and list methods.

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


Re: analyzing time

2013-07-05 Thread Skip Montanaro
> I have a table with a column of type date, with dates and time combined (like 
> '1/6/2013 3:52:69PM'), that spans many months.  How would I pull out records 
> that are the first and last entries per day?

You mentioned "table" and "column", which leads me to think you are
dealing with data in a SQL database.  If so, that would likely change
the problem solution significantly.

If you have something like lists of string data in Python though, you
might want to make sure your timestamps are in a normalized form
first, so you can accurately sort by the timestamps.  For that, my
weapon of choice is the dateutil package, and in particular, the
dateutil.parser module:

>>> x = dateutil.parser.parse("1/6/2013 3:52:59PM")
>>> x
datetime.datetime(2013, 1, 6, 15, 52, 59)
>>> print x
2013-01-06 15:52:59

Once your timestamps are represented as Python datetime objects, the
problem gets a bit easier, especially if you want to find the
beginning and ending timestamps of a bunch of dates.  Sort, then throw
some itertools.groupby pixie dust at it. My ancient, reptilian brain
has never quite grokked all that iterator stuff, so I won't hazard a
guess how to spell the exact solution.

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


Re: Convert SOAP response (ArrayOfInt) to Python list

2013-07-05 Thread Burak Arslan


Hi,

FYI, There's a soap-specific python.org list: s...@python.org


On 07/04/13 20:57, robert.wink...@bioprocess.org wrote:
> Thanks to the OSA library, which works for SOAP requests with Python 3.x, I 
> can now use SOAP services at http://www.chemspider.com.
>
> The results structure is 
>   
> int
> int
>   
>
> The result is a list of accession numbers (which correspond to chemical 
> compounds) and I get them in the following format:
>
> [snip]
>
> How could I transform this to a simple python list?

I did not use OSA, but assuming print(ret) prints that, you should do
ret.int to get your list.
It should already be a regular Python list.

I hope that helps.

Best,
Burak

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


Re: analyzing time

2013-07-05 Thread Terry Reedy

On 7/5/2013 3:18 PM, noydb wrote:


I have a table with a column of type date, with dates and time


This is a datetime in Python parlance.


combined (like '1/6/2013 3:52:69PM'), that spans many months.  How
would I pull out records that are the first and last entries per
day?


Sort on that column. Look at pairs of rows. If the days differ, you have 
the last of the first and the first of the second. One way:


it = 
dt1 = next(it)
d1 = date(dt1)  # whatever that looks like

for row in it:
  dt2 = row
  d2 = date(dt2)
  if d1 != d2:
do_whatever(dt1, dt2)
  dt1, d1 = dt2, d2


Also, if I wanted to find time clusters per day (or per week) -- like
if an entry is made every day around 11am -- is there a way to get at
that temporal statistical cluster?


Make a histogram of time, ignoring date.


Python 2.7, Windows 7.

Any guidance would be greatly appreciated!  Time seems tricky...


Yes

--
Terry Jan Reedy

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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Tim Chase
On 2013-07-05 22:08, Νίκος Gr33k wrote:
> Is there a way to extract out of some environmental variable the
> Geo location of the user being the city the user visits out website
> from?
> 
> Perhaps by utilizing his originated ip address?

Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/

which you can use to either populate an existing database with
the .CSV data there, or use the binary data blob in concert with the
Python API

https://github.com/maxmind/geoip-api-python  # Python + C
http://pypi.python.org/pypi/pygeoip/ # pure Python

Just be sure to adhere to the licensing terms.

-tkc




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


Re: analyzing time

2013-07-05 Thread Gary Herron

On 07/05/2013 12:18 PM, noydb wrote:

Hello All,

I have a table with a column of type date, with dates and time combined (like 
'1/6/2013 3:52:69PM'), that spans many months.  How would I pull out records 
that are the first and last entries per day?

Also, if I wanted to find time clusters per day (or per week) -- like if an 
entry is made every day around 11am -- is there a way to get at that temporal 
statistical cluster?

Python 2.7, Windows 7.

Any guidance would be greatly appreciated!  Time seems tricky...

Thanks,

N


Are you asking a Python question, like how to turn a string "1/6/2013 
3:52:69PM" into an internal representation of time, or are you asking a 
data analysis and statistical question?


If the former, then look at datetime.strptime from the datetime module.

If the later, then you may get an answer here, but I'd suggest trying 
somewhere that discusses statistics and analysis.


Gary Herron

--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Support by Νίκος

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?

Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/

which you can use to either populate an existing database with
the .CSV data there, or use the binary data blob in concert with the
Python API

https://github.com/maxmind/geoip-api-python  # Python + C
http://pypi.python.org/pypi/pygeoip/ # pure Python

Just be sure to adhere to the licensing terms.

-tkc






Thank you ill take a look on

http://pypi.python.org/pypi/pygeoip/  # pure Python

i hope it will be easy!


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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Tim Roberts
? Gr33k  wrote:
>
>Is there a way to extract out of some environmental variable the Geo 
>location of the user being the city the user visits out website from?
>
>Perhaps by utilizing his originated ip address?

It is possible to look up the geographic region associated with a block of
IP addresses.  That does not necessarily bear any resemblence to the actual
location of the user.  It tells you the location of the Internet provider
that registered the IP addresses.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


AMQP listening and user-facing daemon

2013-07-05 Thread Justin Chiu

Hi all,

What is the best approach to writing a concurrent daemon that can 
execute callbacks for different types of events (AMQP messages, parsed 
output of a subprocess, HTTP requests)?


I am considering [twisted][1], the built-in [threading][2] module, and 
[greenlet][3]. I must admit that I am very unfamiliar with concurrent 
programming and Python programming in general (formerly a data analysis 
driven procedural programmer). Any resources on threaded/concurrent 
programming (specifically daemons...not just multi-threading a single 
task) would be much appreciated.


Thank you.

Details:

1) Listens into AMQP messaging queues and executes callbacks when 
messages arrive.
Example: Immediately after startup, the daemon continuously listens to 
the [Openstack Notifications messaging queue][4]. When a virtual machine 
is launched, a notification is generated by Openstack with the hostname, 
IP address, etc. The daemon should read this message and write some info 
to a log (or POST the info to a server, or notify the user...something 
simple).


2) Parse the output of a subprocess and execute callbacks based on the 
output.
Example: Every 30 seconds, a system command "[qstat][5]" is run to query 
a job resource manager (e.g. TORQUE). Similar callbacks to 1).


3) Receive requests from a user and process them. I think this will be 
via WSGI HTTP.
Example: User submits an XML template with virtual machine templates. 
The daemon does some simple XML parsing and writes a job script for the 
job resource manager. The job is submitted to the resource manager and 
the daemon continually checks for the status of the job with "qstat" and 
for messages from AMQP. It should return "live" feedback to the user and 
write to a log.


  [1]: https://twistedmatrix.com/trac/wiki/Documentation
  [2]: http://docs.python.org/2/library/threading.html
  [3]: http://greenlet.readthedocs.org/en/latest/
  [4]: 
https://wiki.openstack.org/wiki/NotificationEventExamples#Immediate_Notifications:

  [5]: http://www.clusterresources.com/torquedocs21/commands/qstat.shtml

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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Dave Angel

On 07/05/2013 04:44 PM, Tim Roberts wrote:

? Gr33k  wrote:


Is there a way to extract out of some environmental variable the Geo
location of the user being the city the user visits out website from?

Perhaps by utilizing his originated ip address?


It is possible to look up the geographic region associated with a block of
IP addresses.  That does not necessarily bear any resemblence to the actual
location of the user.  It tells you the location of the Internet provider
that registered the IP addresses.



To be more specific, the last time I checked, my IP address is 
registered to a location over 1000 miles away.  And it's not a dedicated 
IP address, so it could very well be used tomorrow by someone 200 nmiles 
the other side of the registered location.


Further, I sometimes use other ISP's with different addresses, even when 
I don't leave the house.  And once I leave, anything's possible.


The only things that travel with me and my machine are the cookies.  So 
if someone has asked me my location, and if I told the truth, then that 
information could remain available till I flush my cookies or change 
Virtual Machines or even browsers.


--
DaveA

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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Tim Chase
On 2013-07-05 22:59, Support by Νίκος wrote:
> Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:
> > On 2013-07-05 22:08, Νίκος Gr33k wrote:
> >> Is there a way to extract out of some environmental variable the
> >> Geo location of the user being the city the user visits out
> >> website from?
> >>
> >> Perhaps by utilizing his originated ip address?
> > Yep.  You can get an 11MB database (17MB uncompressed)
> >
> > http://dev.maxmind.com/geoip/legacy/downloadable/
> >
> Thank you ill take a look

Alternatively, you[1] can use JavaScript in your code and have it
submit AJAX requests.  This interacts with the browser, asking it
where (geographically) it thinks it is.  This interacts with a GPS
some browsers (particularly mobile), while on others, it trusts the
user to report where they are.  Note that at any time, the user can
decline to answer or blatantly lie[2].

-tkc


[1]
generic "you"...this requires a fair bit of coding skill and the
ability to read/understand documentation that I can't say I've seen
demonstrated in certain recent threads.

[2]
https://addons.mozilla.org/en-us/firefox/addon/geolocater/








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


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Grant Edwards
On 2013-07-05, ?? Gr33k  wrote:

> Is there a way to extract out of some environmental variable the Geo 
> location of the user being the city the user visits out website from?

No.

> Perhaps by utilizing his originated ip address?

There is a very poor correlation between IP address and geographical
location.  I know that users of the ISPs in hometown are consistently
mis-identified as being from towns 1500km away.

-- 
Grant Edwards   grant.b.edwardsYow! If elected, Zippy
  at   pledges to each and every
  gmail.comAmerican a 55-year-old
   houseboy ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python adds an extra half space when reading from a string or list

2013-07-05 Thread rurpy
On 07/04/2013 06:09 AM, Antoon Pardon wrote:
> Op 03-07-13 19:11, ru...@yahoo.com schreef:
>> On 07/03/2013 03:21 AM, Antoon Pardon wrote:
>>> Op 03-07-13 02:30, ru...@yahoo.com schreef:
 If your going to point out something negative about someone
 then do so politely.  Ask yourself if you were pointing out 
 incompetence to your boss (or anyone else where impoliteness
 could have real consequences for you) if you would say, 
 "you're incompetent." 
>>> And so we shift from no problem speaking bluntly or clearly
>>> to wording it in a way that wouldn't antagonize your boss
>>> too much.
>> As I pointed out, emotionally-loaded, judgmental language 
>> *is not* clear.
> 
> Well that is true, but mostly in the trivial sense that
> language is rarely clear even when you are talking facts.

Its true in more than a trivial sense.

> When I meet someone new and I talk about my love of spaghetti
> and the other inivites me to the spaghetti evening the next
> day, that can turn out to be a big disappointment because
> when I talk about spaghetti, I mean a carbonarra while I
> was invited to a bolognaise-evening.

Of course there is some degree of uncertainty.  But that
uncertainty is relatively small when compared with the
range of possible food you might have been served had 
your evening's companion not specified "spaghetti".

Had he or she invited you to a really "delicious" meal
on the other hand, the uncertainty about the what 
"delicious" means would far greater.  Unless you know 
the person, "delicious" is so subjective as to have very
little meaning since its meaning varies between people
much more than "spaghetti" does.

Describing someone as "stupid", "incompetent", "a 
dick", etc has a similar high degree of subjectivity
and its meaning depends more on the sayer than on any 
objective attribute of the subject.

If you can tell me something objective about the subject,
then that may be helpful to me in deciding how to respond 
to him or her.  If you just spout subjective invective, 
then its just noise because I don't know enough about 
you to trust your judgment.  (Advanced technical python 
knowledge does not qualify one in judging other human 
beings.)  And since I'm reading the thread I have access 
to the same info you do, and can form my own subjective
opinion.  Judging other people is in my opinion a moral 
action that is too important to delegate or to do by just
going along with the crowd.

So such subjective, emotionally-loaded, judgmental responses 
provide little benefit to others, amplify whatever negative
tone was created by the troll and stimulate the troll.  The
only benefits are to you who gets to vent and argue, and a 
(hopefully few) voyeurs and fellow vigilantes who enjoy 
watching and joining in on that kind of flamage.

>>  And yes, I think "wouldn't antagonize your
>> boss" is not a bad heuristic for judging the politeness of 
>> your response.
> 
> That may be true for you personnally, but you are unsufficiently
> clear for anyone else to be of any help. The problem is that
> when you wrote this, you had a specific kind of boss in mind [...]

No.  I originally wrote "your boss (or anyone else where 
impoliteness could have real consequences for you)."  I 
believe most people would read that as intended, some sort
of generalized authority figure, if not boss then maybe a
police officer, or a powerful politician that could quaff 
your permit application, or the touchy father of a woman 
you want to date, and not necessarily "Mr. Joel Davis,
my boss at this job at this moment and also my golfing 
buddy."

> who would react in certain ways to certain kinds of treatment.
> However it would be extremely unlikely that other people
> would come up with the same idea of boss. And not everybody
> is in the same situation, some people can't afford to lose
> there job, others are in a less desperate situation, for
> some people their priority is their career, while for others
> it is the service to their clients. All these people are going
> to come up with wildly different answers.

The answers will presumably share a common characteristic:
the need to address the person with politeness and respect,
even when expressing disagreement with them.

>> Again I ask, what does his web site admin skills or lack
>> thereof have to do with python?
>>
>> If you want to decline providing Python help to someone 
>> because you don't like some real-world behavior of the 
>> person, fine.  But when you attack him over it, and 
>> publicly engage in a long, noisy discussion here in 
>> which you trumpet your moral and technical superiority,
>> then you should not be surprised when the target takes 
>> offense and responds in kind.
>>
>> If someone from Wikileaks posts here seeking Python help, 
>> should we engage in a long discussion about the morality 
>> of Wikileaks and how they aid US fugitives from justice?
>>
>> How about someone who lets slip he's just been released 
>> from priso

Re: python adds an extra half space when reading from a string or list

2013-07-05 Thread rurpy
On 07/04/2013 02:50 AM, feedthetr...@gmx.de wrote:
> Am Mittwoch, 3. Juli 2013 19:01:23 UTC+2 schrieb ru...@yahoo.com:
>[...]
 Any questions?
>> Yes.  
>[...]
>> I know the answers to all these questions are obvious 
>> to everyone else here but I am not sure about them.
> 
> Then I hope, I was able to enlighten you. ;-)

as i said, i love the internet, it's so easy to get answers 
to all those questions i thought were difficult and complex.  
You cleared everything up in just a few sentences, thanks.
i'm now going to go read the truth about the 9-11 attacks.
-- 
http://mail.python.org/mailman/listinfo/python-list


calculating binomial coefficients using itertools

2013-07-05 Thread Robert Hunter
from itertools import count, repeat, izip, starmap

def binomial(n):
"""Calculate list of Nth-order binomial coefficients using itertools."""

l = range(2)
for _ in xrange(n):
indices = izip(count(-1), count(1), repeat(1, len(l) + 1))
slices = starmap(slice, indices)
l = [sum(l[s]) for s in slices]
return l[1:]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Important features for editors

2013-07-05 Thread Cameron Simpson
On 05Jul2013 05:12, rusi  wrote:
| On Thursday, July 4, 2013 1:37:10 PM UTC+5:30, Göktuğ Kayaalp wrote:
| > Programmability comes to my mind, before anything else.  I'd suggest
| > to find out about designs of Emacs and Vi(m).
| 
| There's one reason I prefer emacs -- and I guess some people
| prefer Idle -- the interpreter and editor are tightly integrated.

That is indeed a strength of emacs over vi.

For myself, I generally don't want to program my editor beyond writing
keyboard macros, and vim's programming interface has yet to attract me.

When I want to manipulate text beyond a simple macro I tend to write
a sed script. Or awk, or python in increasing complexity of task.

[...]
| One expansion for EMACS is Editor for Middle Aged Computer
| Scientists -- so I am guessing if you're asking the question you
| dont qualify :-)

While I started with vi just slightly before encountering emacs
(mid-to-late 1980s, both), my main trouble with choosing emacs was
the heavy use of control keys. Vi's modal nature means that in
"edit" mode, all the keystrokes are available as edit controls.
Emacs' modeless nature means that all the edit controls must be
control-this and meta/escape-that.

For this reason, I often expand EMACS as Escape Meta Alt Control Shift.

I'm a vi user. Once I mastered "hit ESC by reflex when you pause
typing an insert" I was never confused above which mode I was in.

And now my fingers know vi.

Cheers,
-- 
Cameron Simpson 

A novice of the temple once approached the Chief Priest with a question.

  "Master, does Emacs have the Buddha nature?" the novice asked.

  The Chief Priest had been in the temple for many years and could be relied
  upon to know these things.  He thought for several minutes before replying.

  "I don't see why not.  It's got bloody well everything else."

  With that, the Chief Priest went to lunch.  The novice suddenly achieved
enlightenment, several years later.

Commentary:

His Master is kind,
Answering his FAQ quickly,
With thought and sarcasm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?


Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/



http://pypi.python.org/pypi/pygeoip/ # pure Python


Thank you very much Tim.
i am know tryitn to use it as:

import pygeoip

try:
  gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
  host = gic.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
  host = repr(e)

lets hope it will work!


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 6/7/2013 2:55 πμ, ο/η Νίκος Gr33k έγραψε:

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?


Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/



http://pypi.python.org/pypi/pygeoip/ # pure Python


Thank you very much Tim.
i am know trying to use it as:

import pygeoip

try:
   gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
   host = gic.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
   host = repr(e)

lets hope it will work!


Just my luck again,

PermissionError(13, 'Άρνηση πρόσβασης')

Άρνηση πρόσβασης = Access Denied

Why would that happen?


--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Geo Location extracted from visitors ip address

2013-07-05 Thread Νίκος Gr33k

Στις 6/7/2013 2:58 πμ, ο/η Νίκος Gr33k έγραψε:

Στις 6/7/2013 2:55 πμ, ο/η Νίκος Gr33k έγραψε:

Στις 5/7/2013 10:58 μμ, ο/η Tim Chase έγραψε:

On 2013-07-05 22:08, Νίκος Gr33k wrote:

Is there a way to extract out of some environmental variable the
Geo location of the user being the city the user visits out website
from?

Perhaps by utilizing his originated ip address?


Yep.  You can get an 11MB database (17MB uncompressed)

http://dev.maxmind.com/geoip/legacy/downloadable/



http://pypi.python.org/pypi/pygeoip/ # pure Python


Thank you very much Tim.
i am know trying to use it as:

import pygeoip

try:
   gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
   host = gic.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
except Exception as e:
   host = repr(e)

lets hope it will work!


Just my luck again,

PermissionError(13, 'Άρνηση πρόσβασης')

Άρνηση πρόσβασης = Access Denied

Why would that happen?


root@nikos [~]# ls -l GeoLiteCity.dat
-rw-r--r-- 1 root root 17633968 Jul  3 02:11 GeoLiteCity.dat
root@nikos [~]# chmod +x GeoLiteCity.dat
root@nikos [~]# ls -l GeoLiteCity.dat
-rwxr-xr-x 1 root root 17633968 Jul  3 02:11 GeoLiteCity.dat*
root@nikos [~]# python
Python 3.3.2 (default, Jun  3 2013, 16:18:05)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygeoip
>>> gic = pygeoip.GeoIP('/root/GeoIPCity.dat')
Traceback (most recent call last):
  File "", line 1, in 
  File 
"/usr/local/lib/python3.3/site-packages/pygeoip-0.2.6-py3.3.egg/pygeoip/__init__.py", 
line 110, in __init__

self._filehandle = codecs.open(filename, 'rb', ENCODING)
  File "/usr/local/lib/python3.3/codecs.py", line 884, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: 
'/root/GeoIPCity.dat'

>>>



--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >