Re: UnicodeDecodeError issue

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus  wrote:
> Ye i'm aware that i need to define variables before i try to make use of them.
> I have study all of your examples and then re-view my code and i can *assure* 
> you that the line statement that tied to set the 'host' variable is very 
> early at the top of the script(of course after imports), and the cur.execute 
> comes after.
>
> The problem here is not what you say, that i try to drink k a coffee before 
> actually making one first but rather than i cannot drink the coffee although 
> i know *i have tried* to make one first.
>
>
> i will upload the code for you to prove my sayings at pastebin.
>
> http://pastebin.com/J97guApQ

You're setting host inside a try/except block. Are you getting
exceptions that prevent it from being set, perhaps?

Also... do you seriously (a) block access if no Referer: header, and
(b) permit that access anyway if the user has a non-blank cookie named
'admin'? Seriously??

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 10:12 πμ, ο/η Chris Angelico έγραψε:

On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus  wrote:

Ye i'm aware that i need to define variables before i try to make use of them.
I have study all of your examples and then re-view my code and i can *assure* 
you that the line statement that tied to set the 'host' variable is very early 
at the top of the script(of course after imports), and the cur.execute comes 
after.

The problem here is not what you say, that i try to drink k a coffee before 
actually making one first but rather than i cannot drink the coffee although i 
know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ


You're setting host inside a try/except block. Are you getting
exceptions that prevent it from being set, perhaps?

Also... do you seriously (a) block access if no Referer: header, and
(b) permit that access anyway if the user has a non-blank cookie named
'admin'? Seriously??

ChrisA



I have checked th output of the erro log a tthe very moment that 
files.py tris to run and i receive this:



[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] ValueError: 
underlying buffer has been detached, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] , referer: 
http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] Original 
exception was:, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] Traceback 
(most recent call last):, referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113]   File 
"/home/nikos/public_html/cgi-bin/files.py", line 135, in , 
referer: http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] 
cur.execute('''INSERT INTO files (url, host, city, lastvisit) VALUES 
(%s, %s, %s, %s)''', (filename, host, city, lastvisit) ), referer: 
http://superhost.gr/
[Sun Sep 01 07:17:52 2013] [error] [client 108.162.231.113] NameError: 
name 'host' is not defined, referer: http://superhost.gr/


Let alone that i when i try to set the 'host' variable i get this line 
at my '/tmp/err.out'



[email protected] [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
'invalid start byte')


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


Re: UnicodeDecodeError issue

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 5:23 PM, Ferrous Cranus  wrote:
> Let alone that i when i try to set the 'host' variable i get this line at my
> '/tmp/err.out'
>
>
> [email protected] [~]# cat /tmp/err.out
>
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
> 'invalid start byte')

So host isn't being set because of this error, which you're blithely
carrying on after and assuming that things have happened. Solve this
problem, only then move on. Otherwise you WILL confuse yourself
further.

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Steven D'Aprano
On Sat, 31 Aug 2013 05:57:47 -0700, Fabrice Pombet wrote:

> Steve, I think that your definition of encapsulation is too wide to give
> a reasonable answer to the question at hand. 

My definition of encapsulation is based on the plain language definition. 
It is also common in computer programming circles to talk about 
encapsulation in this way. OOP designers may (or may not) have been the 
first to declare "encapsulation" was a design principle, but programmers 
have been using the *principle* of encapsulation since before Grace 
Hopper was an admiral.

See, for example, coders talking about encapsulation in C and Powershell:

http://ejrh.wordpress.com/2011/04/29/encapsulation-in-c/

https://www.simple-talk.com/dotnet/.net-tools/further-down-the-rabbit-
hole-powershell-modules-and-encapsulation/

OOP languages give us *more* and *better* ways to encapsulate code and 
data, but they did not invent the principle.


> If I understand you
> correctly, you are taking encapsulation as a language characteristic, 
> rather than a principle. 

No, it is both. The coder may or may not decide to encapsulate code in 
subroutines/functions/classes, and the language may or may not allow it.

Languages differ in their ability to allow the programmer to encapsulate. 
Some languages, like early BASIC, give you no ability to encapsulate code 
or data at all. All variables are global, and there are no functions, 
just a big blob of code in a single file. There aren't even data 
structures as such, except strings, so you cannot even group related 
pieces of data into a struct or record.

Some languages, like Python, give you many ways to encapsulate code and 
data: you can group related code in a function, related functions in a 
class, related classes in a module, related modules in a package. That's 
pretty much exactly the same sort of things that you can do in Java. C++ 
has an additional "namespace" data structure that Python doesn't have, 
but that's just a mechanism for encapsulation.

Encapsulation and information hiding are distinct things -- you can have 
one without the other. C, for example, creates a new scope inside for-
loops, so that the for-loop variable is hidden from the rest of the 
function. Apart from a pair of braces, there is no encapsulation, but 
there is information hiding. Or you could design a language that 
encapsulated code into functions and classes, but put all variables in a 
single, global, shared namespace (little, or no, information hiding).

It is a myth, and a very obnoxious one, that encapsulation and 
information hiding were invented by OOP. What is a function but a way to 
hide the implementation of a chunk of code from the caller? What are 
local variables but a way to hide variables used by one function from 
another? Coders were using information hiding, separation of concerns, 
and encapsulation in the 1950s, long before OOP. They just didn't call 
them by those names. They just called them "writing good code".

Actually, functions are *not necessarily* a way to hide implementation. 
There are languages where you can jump into the middle of a function. So 
you can encapsulate a chunk of code into a function, without hiding the 
implementation details. Just as you can encapsulate code and data into a 
class, without hiding the implementation details, if you declare 
everything public.


> Plus, you seem to forget that encapsulation is
> an OOP principle, and, forgive me if I am wrong, does not apply normally
> to functions or languages like C. 

I haven't forgotten it, because it isn't true.

One of the most obnoxious and annoying traits of OOP zealots, especially 
academics, is that they act as if programming did not exist before Java 
and C++, or if you're really lucky, Smalltalk. (Somehow they nearly 
always forget about Simula.) Perhaps because OOP was so late to be 
invented (structured programming goes back to Fortran in the 1950s, 
functional programming to Lisp only a few years after that), and because 
it was so heavily hyped as "the solution" to every programming 
difficulty, too many people ignore anything outside of the OOP. They 
wrongly believe that since Python isn't a "pure" OOP language (according 
to some bizarre understanding of "pure" that often considers C++ and Java 
pure), Python cannot possibly have "OOP principles" like encapsulation, 
information hiding, separation of concerns.

That point of view is sheerest nonsense.


> Please read Steve Holden's (in chaz')
> definition, and tell us whether you think that Python enforces strongly
> this principle, I think that would be a good basis for an agreement. 

Are you referring to this quote?

"encapsulation is the idea that the only way to access or change the data 
inside an object is by calling its methods."


I disagree with that definition. That's a poor definition, one that has 
no relation to the plain English meaning of the word "encapsulation", nor 
to how the word is commonly used in the great bulk 

Re: Encapsulation unpythonic?

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 6:10 PM, Steven D'Aprano
 wrote:
> Java and C++ allow you to declare members as public, so it is *not true*
> that calling methods is the only way to change members. If you accept
> Steve Holden's (wrong) definition above, Java and C++ don't have
> encapsulation either.

That said, though, when you consider the language ecosystem rather
than just the language, there is a strong tendency for Java and C++
code to wrap everything up with functions (no public data members),
whereas Python code is far more likely to have external code directly
access data inside an object. You usually will find Java code calling
methods to change members, whereas that's done in Python only when
there's a need for it.

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Steven D'Aprano
On Sat, 31 Aug 2013 23:50:23 -0700, Ferrous Cranus wrote:

> Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous
> Cranus έγραψε:
>> Suddenly my webiste superhost.gr running my main python script presents
>> 
>> me with this error:
>> 
>> 
>> 
>> Code:
>> 
>> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
>> 
>> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
>> 
>> 'invalid start byte')
>> 
>> 
>> 
>> 
>> 
>> Does anyone know what this means?
>> 
>> 
>> 
>> 
>> 
>> --
>> 
>> Webhost 
> 
> Good morning Steven,
> 
> Ye i'm aware that i need to define variables before i try to make use of
> them. I have study all of your examples and then re-view my code and i
> can *assure* you that the line statement that tied to set the 'host'
> variable is very early at the top of the script(of course after
> imports), and the cur.execute comes after.
> 
> The problem here is not what you say, that i try to drink k a coffee
> before actually making one first but rather than i cannot drink the
> coffee although i know *i have tried* to make one first.
> 
> 
> i will upload the code for you to prove my sayings at pastebin.
> 
> http://pastebin.com/J97guApQ


You are mistaken. In line 20-25, you have this:

try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] 
or "Proxy Detected"
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what 
the error is? You have access to the err.out file, but apparently you 
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of "host" 
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and 
fix it.


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


Re: Encapsulation unpythonic?

2013-09-01 Thread Fabrice Pombet
 > That said, though, when you consider the language ecosystem rather
> 
> than just the language, there is a strong tendency for Java and C++
> 
> code to wrap everything up with functions (no public data members),
> 
> whereas Python code is far more likely to have external code directly
> 
> access data inside an object. You usually will find Java code calling
> 
> methods to change members, whereas that's done in Python only when
> 
> there's a need for it.


Yep, this is precisely my point, if you take encapsulation as a philosophical 
principle, Java and C++ would tend to be abiding by it, as a "default" setting 
that you can at times change, whereas python would tend to be the contrary. In 
other words, you can set some encapsulation if and when you want to, but you 
can leave your code without it when it's not needed/inconvenient. So I guess 
that we are actually all agreeing on this one.

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 03:23, Ferrous Cranus wrote:

> Στις 1/9/2013 10:12 πμ, ο/η Chris Angelico έγραψε:
>> On Sun, Sep 1, 2013 at 4:50 PM, Ferrous Cranus  wrote:
>>> Ye i'm aware that i need to define variables before i try to make use of 
>>> them.
>>> I have study all of your examples and then re-view my code and i can 
>>> *assure* you that the line statement that tied to set the 'host' variable 
>>> is very early at the top of the script(of course after imports), and the 
>>> cur.execute comes after.
>>>
>>> The problem here is not what you say, that i try to drink k a coffee before 
>>> actually making one first but rather than i cannot drink the coffee 
>>> although i know *i have tried* to make one first.
>>>
>>>
>>> i will upload the code for you to prove my sayings at pastebin.
>>>
>>> http://pastebin.com/J97guApQ
>>
>> You're setting host inside a try/except block. Are you getting
>> exceptions that prevent it from being set, perhaps?
>>

   
>
> Let alone that i when i try to set the 'host' variable i get this line 
> at my '/tmp/err.out'
>
>
> [email protected] [~]# cat /tmp/err.out
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
> 'invalid start byte')
>

Let's examine the relevant code from your pastebin above:


try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy 
Detected"
except Exception as e:
print( repr(e), file=open( '/tmp/err.out', 'w' ) )
 
Presumably you do realize that any exception on any of the three lines
will skip the remainder, and print that line to the /tmp/err.out file? 
And that if any exception occurs, it'll be before 'host' is initialized
at all?  Possibly before 'city' is initialized, or even 'gi' ?   So
there's no reason yet to assume that somehow setting the 'host'
variable triggers anything. When in doubt, consider including only one
line in any given try block. But let's try simpler changes.

Your file mode is 'w' which will wipe out anything written earlier.  Yet
if the exception does not happen, the file will still contain error(s)
from some earlier run.  You should open (and truncate) the file before
the first use, then use the same handle in each exception at which you
want to record any information.  Further, you ought to put more into the
file than just the repr(e) value.  For starters, the Traceback would be
nice.

If I were doing it, I'd be printing  repr(sys.exc_info()) to that file. 
I'd also have some kind of label, so that once I had more than one of
these in the code, I'd have an easyt way to tell which was which.

This is my first crack at it (untested):

errout = open("/tmp/err.out", "w")   #opens and truncates the error
output file
try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
"Proxy Detected"
except Exception as e:
print( "Xyzzy exception-", repr(sys.exc_info()), file=errout)
errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier.  But right now, you need to stop throwing
away useful information.


-- 
DaveA


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


Re: UnicodeDecodeError issue

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 8:35 PM, Dave Angel  wrote:
> Your file mode is 'w' which will wipe out anything written earlier.  Yet
> if the exception does not happen, the file will still contain error(s)
> from some earlier run.  You should open (and truncate) the file before
> the first use, then use the same handle in each exception at which you
> want to record any information.

Hmm, I'd go the other way, and append to the file every time there's
an error - otherwise, one successful page load will wipe out the error
from a previous one. But I agree that the inconsistency is not helping
him.

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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread Roy Smith
In article <[email protected]>,
 [email protected] wrote:

> It is the call to gethostbyname_ex that is very slow.  The call to 
> gethostname is quick (and returns the same string as /usr/bin/hostname).

First, please stop posting with Google Groups.  It makes a total mess of 
the quoted text.

Next, what happens when you use the command-line tools (nslookup or dig) 
to translate your hostname to an IP address, and what happens if you 
then try to reverse translate that IP address back to a name?

> This issue is independent of IPython:
> 
> $ time python -c 'import socket; 
> print(socket.gethostbyname_ex(socket.gethostname())[2])' 
> ['192.168.0.102']
> python -c   0.07s user 0.02s system 0% cpu 28.190 total

The real point is not that it's independent of IPython, it has nothing 
to do with Python at all.  What you've got here is a network 
configuration issue.  You would do better to recreate this problem with 
the native OS command line tools and then ask about it on a forum 
dedicated to your operating system.
-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie: use of built-in exceptions

2013-09-01 Thread Rui Maciel
Are there any guidelines on the use (and abuse) of Python's built-in 
exceptions, telling where 
it's ok to raise them and where it's preferable to define custom exceptions 
instead?  


Thanks in advance,
Rui Maciel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: use of built-in exceptions

2013-09-01 Thread Chris “Kwpolska” Warrick
On Sun, Sep 1, 2013 at 1:17 PM, Rui Maciel  wrote:
> Are there any guidelines on the use (and abuse) of Python's built-in 
> exceptions, telling where
> it's ok to raise them and where it's preferable to define custom exceptions 
> instead?

There are no rules.  You should use common sense instead: if the
exception fits your needs (eg. ValueError when incorrect output
occurs) then use it.

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread Chris Angelico
On Sun, Sep 1, 2013 at 10:03 AM,   wrote:
> At startup, IPython (qtconsole) calls 
> "socket.gethostbyname_ex(socket.gethostname())[2]" to find a list of IP 
> addresses that point to the machine. On a Linux server that I manage this 
> call is extremely slow (>20s)... which I have trouble understanding as "ip 
> addr show" seems to give the same information nearly instantaneously. Is 
> there anything I can do to make this faster? Can this be a network 
> configuration issue (I am behind a router)?

Yes, it most definitely CAN be a network config issue. The C function
you want to be calling is getifaddrs(), and I don't think there's a
way to call that from core Python. But a Google search for 'python
getifaddrs' shows up a few third-party modules that might be of use to
you; that'd be a lot quicker and more reliable than trying to look up
your own hostname and depending on the results.

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 1:35 μμ, ο/η Dave Angel έγραψε:

This is my first crack at it (untested):

errout = open("/tmp/err.out", "w")   #opens and truncates the error
output file
try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
"Proxy Detected"
except Exception as e:
 print( "Xyzzy exception-", repr(sys.exc_info()), file=errout)
 errout.flush()


Note that I haven't had to use exc_info() in my own code, so I'm sure it
could be formatted prettier.  But right now, you need to stop throwing
away useful information.


First of all thank you for your detailed information Dave.
I have tried all you said, the above example you provided me, but i'm 
afraid even with your approach which should have given more error 
specific information the output of the err file remains.



[email protected] [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
'invalid start byte')




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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 11:35 πμ, ο/η Steven D'Aprano έγραψε:

On Sat, 31 Aug 2013 23:50:23 -0700, Ferrous Cranus wrote:


Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous
Cranus έγραψε:

Suddenly my webiste superhost.gr running my main python script presents

me with this error:



Code:

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

\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,

'invalid start byte')





Does anyone know what this means?





--

Webhost 


Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of
them. I have study all of your examples and then re-view my code and i
can *assure* you that the line statement that tied to set the 'host'
variable is very early at the top of the script(of course after
imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee
before actually making one first but rather than i cannot drink the
coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ



You are mistaken. In line 20-25, you have this:

try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
 socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
 or "Proxy Detected"
except Exception as e:
 print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what
the error is? You have access to the err.out file, but apparently you
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of "host"
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and
fix it.




But i'm Steven! That why i make use of it to read it immediately after 
my script run at browser time.


i have even included a sys.exit(0) after the try:/except block:

Here is it:


errout = open( '/tmp/err.out', 'w' )		# opens and truncates the error 
output file

try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
	city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
	host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy 
Detected"

except Exception as e:
print( "Xyzzy exception-", repr( sys.exc_info() ), file=errout )
errout.flush()

sys.exit(0)

and the output of error file is:


[email protected] [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
'invalid start byte')


--
Webhost 

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 5:08 μμ, ο/η Ferrous Cranus έγραψε:

Στις 1/9/2013 11:35 πμ, ο/η Steven D'Aprano έγραψε:

On Sat, 31 Aug 2013 23:50:23 -0700, Ferrous Cranus wrote:


Τη Σάββατο, 31 Αυγούστου 2013 9:41:27 π.μ. UTC+3, ο χρήστης Ferrous
Cranus έγραψε:

Suddenly my webiste superhost.gr running my main python script presents

me with this error:



Code:

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

\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,

'invalid start byte')





Does anyone know what this means?





--

Webhost 


Good morning Steven,

Ye i'm aware that i need to define variables before i try to make use of
them. I have study all of your examples and then re-view my code and i
can *assure* you that the line statement that tied to set the 'host'
variable is very early at the top of the script(of course after
imports), and the cur.execute comes after.

The problem here is not what you say, that i try to drink k a coffee
before actually making one first but rather than i cannot drink the
coffee although i know *i have tried* to make one first.


i will upload the code for you to prove my sayings at pastebin.

http://pastebin.com/J97guApQ



You are mistaken. In line 20-25, you have this:

try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
 gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
 socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0]
 or "Proxy Detected"
except Exception as e:
 print( repr(e), file=open( '/tmp/err.out', 'w' ) )


An error occurs inside that block, *before* host gets set. Who knows what
the error is? You have access to the err.out file, but apparently you
aren't reading it to find out.

Then, 110 lines later, at line 135, you try to access the value of "host"
that never got set.

Your job is to read the error in /tmp/err.out, see what is failing, and
fix it.




But i'm Steven! That why i make use of it to read it immediately after
my script run at browser time.

i have even included a sys.exit(0) after the try:/except block:

Here is it:


errout = open( '/tmp/err.out', 'w' )# opens and truncates the
error output file
try:
 gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
 city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy
Detected"
except Exception as e:
 print( "Xyzzy exception-", repr( sys.exc_info() ), file=errout )
 errout.flush()

sys.exit(0)

and the output of error file is:


[email protected] [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')




But i noticed that err.out and /usr/local/apache/logs/error_log produced 
different output.


In any case i check both:


[email protected] [~]# chmod 777 /tmp/err2.out

ouput of error_log
[email protected] [~]# [Sun Sep 01 14:23:46 2013] [error] [client 
173.245.49.120] Premature end of script headers: metrites.py
[Sun Sep 01 14:23:46 2013] [error] [client 173.245.49.120] File does not 
exist: /home/nikos/public_html/500.shtml




Also i have even changed output error filename.
turns out empty.

[email protected] [~]# cat /tmp/err2.out

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 10:08, Ferrous Cranus wrote:

   
> Here is it:
>
>
> errout = open( '/tmp/err.out', 'w' )  # opens and truncates the error 
> output file
> try:
>   gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
>   city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or 
> gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
>   host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or 
> socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy 
> Detected"
> except Exception as e:
>   print( "Xyzzy exception-", repr( sys.exc_info() ), file=errout )
>  errout.flush()
>
> sys.exit(0)
>
> and the output of error file is:
>
>
> [email protected] [~]# cat /tmp/err.out
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
> 'invalid start byte')
>

Nope.  The label  "Xyzzy exception" is not in that file, so that's not
the file you created in this run.  Further, if that line existed before,
it would have been wiped out by the open with mode "w".

i suggest you add yet another write to that file, immediately after
opening it:

errout = open( '/tmp/err.out', 'w' )# opens and truncates the error 
print("starting run", file=errorout)
errout.flush()

Until you can reliably examine the same file that was logging your
errors, you're just spinning your wheels.  you might even want to write
the time to the file, so that you can tell whether it was now, or 2 days
ago that the run was made.


-- 
DaveA


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


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 09:59, Ferrous Cranus wrote:

> Στις 1/9/2013 1:35 μμ, ο/η Dave Angel έγραψε:
>> This is my first crack at it (untested):
>>
>> errout = open("/tmp/err.out", "w")   #opens and truncates the error
>> output file
>> try:
>>  gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
>>  city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
>> gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
>>  host =socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
>> socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or
>> "Proxy Detected"
>> except Exception as e:
>>  print( "Xyzzy exception-", repr(sys.exc_info()), file=errout)
>>  errout.flush()
>>
>>
>> Note that I haven't had to use exc_info() in my own code, so I'm sure it
>> could be formatted prettier.  But right now, you need to stop throwing
>> away useful information.
>
> First of all thank you for your detailed information Dave.
> I have tried all you said, the above example you provided me, but i'm 
> afraid even with your approach which should have given more error 
> specific information the output of the err file remains.
>
>
> [email protected] [~]# cat /tmp/err.out
> UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef 
> \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1, 
> 'invalid start byte')
>
>
>

See my other response.  The above file did NOT result from running the
code above.  It is missing the "Xyzzy" label.

-- 
Signature file not found

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 6:36 μμ, ο/η Dave Angel έγραψε:

On 1/9/2013 10:08, Ferrous Cranus wrote:



Here is it:


errout = open( '/tmp/err.out', 'w' )# opens and truncates the error
output file
try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy
Detected"
except Exception as e:
print( "Xyzzy exception-", repr( sys.exc_info() ), file=errout )
  errout.flush()

sys.exit(0)

and the output of error file is:


[email protected] [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')



Nope.  The label  "Xyzzy exception" is not in that file, so that's not
the file you created in this run.  Further, if that line existed before,
it would have been wiped out by the open with mode "w".

i suggest you add yet another write to that file, immediately after
opening it:

errout = open( '/tmp/err.out', 'w' )# opens and truncates the error
print("starting run", file=errorout)
errout.flush()

Until you can reliably examine the same file that was logging your
errors, you're just spinning your wheels.  you might even want to write
the time to the file, so that you can tell whether it was now, or 2 days
ago that the run was made.





I tried it and it printed nothing.
But suddenly thw ebpage sttaed to run and i get n invalid byte entried 
and no weird messge files.py is working as expcted.

what on earht?

Now i ahve thso error:

# 
=
# DATABASE INSERTS - do not increment the counter if a Cookie is set to 
the visitors browser already
# 
=
if( not vip and re.search( 
r'(msn|gator|amazon|yandex|reverse|cloudflare|who|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None ):


print( "i'm in and data is: ", host )
try:
#find the needed counter for the page URL
if os.path.exists( path + page ) or os.path.exists( cgi_path + 
page ):
cur.execute('''SELECT ID FROM counters WHERE url = 
%s''', page )
data = cur.fetchone()   #URL is unique, so 
should only be one

if not data:
#first time for page; primary key is automatic, hit is 
defaulted
cur.execute('''INSERT INTO counters (url) VALUES 
(%s)''', page )
cID = cur.lastrowid#get the primary key value 
of the new record
else:
#found the page, save primary key and use it to issue 
hit UPDATE
cID = data[0]
			cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID = %s''', 
cID )


#find the visitor record for the (saved) cID and current host
		cur.execute('''SELECT * FROM visitors WHERE counterID = %s and host = 
%s''', (cID, host) )

data = cur.fetchone()#cID&host are unique

if not data:
#first time for this host on this page, create new 
record
			cur.execute('''INSERT INTO visitors (counterID, host, city, useros, 
browser, lastvisit) VALUES (%s, %s, %s, %s, %s, %s)''', (cID, host, 
city, useros, browser, date) )

else:
#found the page, save its primary key for later use
vID = data[0]
#UPDATE record using retrieved vID
			cur.execute('''UPDATE visitors SET city = %s, useros = %s, browser = 
%s, hits = hits + 1, lastvisit = %s
	WHERE counterID = %s and host = %s''', (city, useros, browser, 
date, vID, host) )


con.commit()#if we made it here, the transaction is 
complete

except pymysql.ProgrammingError as e:
print( repr(e) )
con.rollback()  #something failed, rollback the entire 
transaction
sys.exit(0)


i get no counter increment when visitors visit my webpage.
What on eart is going on?

How the previous error with the invalid byte somehtign got solved?

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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread anntzer . lee
On Saturday, August 31, 2013 10:06:43 PM UTC-7, Michael Torrie wrote:
> On 08/31/2013 10:51 PM, [email protected] wrote:
> 
> > It is the call to gethostbyname_ex that is very slow.  The call to
> > gethostname is quick (and returns the same string as
> > /usr/bin/hostname).
> 
> What gethostbyname_ex and /usr/bin/hostname do are very different
> things.  gethostbyname_ex does a DNS lookup against a server.
> /usr/bin/hostname just checks a local computer setting.  I don't see why
> you are comparing the two.  /usr/bin/hostname is not going to help you
> find a list of IP addresses that point to a machine.

I was just replying to the previous comment "name = socket.gethostname() see 
how long that takes and what it returns.  Then, assuming it returns a string 
containing your hostname (massive handwave about what that actually means)", 
saying that gethostname resolves my own hostname instantaneously.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semicolon at end of python's statements

2013-09-01 Thread Antoon Pardon

Op 31-08-13 02:09, Steven D'Aprano schreef:

On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:


On 29 Aug 2013 23:20, "Ben Finney"  wrote:


Fábio Santos  writes:


It is a shame that this is not possible in python. for..if exists in
comprehensions and not in regular loops but that would be nice
sometimes.


So you use it in a generator expression, and iterate over the
generator:

 for foo in (spam for spam in sequence if predicate(spam)):
 process(spam)

That way, there's no need for new syntax.


The problem I have with that strategy is that it is repetitive and
hinders readability. You wrote "for" and "in" twice, and spam (a pretty
useless intermediate variable) thrice!


There is no need for spam to be "intermediate", and the fact that it
shouldn't be is demonstrated by Ben's error in referring to "process
(spam)" instead of "process(foo)".

We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
 if predicate(spam):
 process(spam)

# Option 2
for spam in filter(predicate, sequence):
 process(spam)

# Option 3
for spam in (spam for spam in sequence if predicate(spam)):
 process(spam)


Adding a fourth option:

for spam in sequence if predicate(spam):
 process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.


I find this rather disenginuous. Dare to suggest here that python might
benetif by incluidng end markers for its suits because it would make it 
clearer how many indent levels were done, and chances are someone here

will suggest that too many indent levels are a sign of bad coding
practice, but when someone suggests a change that would allow him
to structure his program more like how he sees it and indent levels
are not short in supply, suggesting there is no problem, no matter how
many one uses.

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


What does mean @ sign in first of statement

2013-09-01 Thread Mohsen Pahlevanzadeh
Dear all,

What does mean @ sign in first of statement such as:

//
@hybrid_property
def fullname(self):
return self.firstname + " " + self.lastname
///

Sorry for cheap question.

Yours,
Mohsen

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


Re: What does mean @ sign in first of statement

2013-09-01 Thread Chris “Kwpolska” Warrick
On Sun, Sep 1, 2013 at 8:53 PM, Mohsen Pahlevanzadeh
 wrote:
> Dear all,
>
> What does mean @ sign in first of statement such as:
>
> //
> @hybrid_property
> def fullname(self):
> return self.firstname + " " + self.lastname
> ///
>
> Sorry for cheap question.
>
> Yours,
> Mohsen
>
> --
> http://mail.python.org/mailman/listinfo/python-list

@hybrid_property is a decorator.  Great resource:
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/

--
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 03:09 AM, Fabrice Pombet wrote:


So I guess that we are actually all agreeing on this one.


No, we are not.

"encapsulation" != "inaccessible except by getters/setters"

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article ,
 Ethan Furman  wrote:

> On 09/01/2013 03:09 AM, Fabrice Pombet wrote:
> >
> > So I guess that we are actually all agreeing on this one.
> 
> No, we are not.
> 
> "encapsulation" != "inaccessible except by getters/setters"

Nothing is accessible in Python except via getters and setters.  The 
only difference between Python and, say, C++ in this regard is that the 
Python compiler writes them for you most of the time and doesn't make 
you put ()'s at the end of the name :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


connecting clicked signal to different slots depending on function executing

2013-09-01 Thread tausciam
I have one tablewidget I want to use for two different uses, audio and video. 
If cover art is displayed and the user clicks the cover art, it emits a cell 
clicked and gets the list of the songs. If video thumbnails are displayed, it 
emits a cell clicked and plays the video fullscreen.

So, I have the same signal going to two different slots. Is there any way to 
break that connection before I define it again? In other words, in the audio 
section I could have something like:

[break video signal/slot connection]
self.tableWidget.cellClicked.connect(self.audiocell_clicked)

and then for the video section:

[break audio signal/slot connection]
self.tableWidget.cellClicked.connect(self.videocell_clicked)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: connecting clicked signal to different slots depending on function executing

2013-09-01 Thread tausciam
Nevermind. I found that it would let me create the connection again then 
when I disconnected, it would disconnect all of the instances...

so I ended up with: 

self.tableWidget.cellClicked.connect(self.videocell_clicked)   
self.tableWidget.cellClicked.disconnect(self.videocell_clicked)   
self.tableWidget.cellClicked.connect(self.audiocell_clicked)

If there's a better way to do this, I'd be interested in hearing about it. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: connecting clicked signal to different slots depending on function executing

2013-09-01 Thread tausciam
I should add that I know about:

self.tableWidget.cellClicked.disconnect(self.videocell_clicked)  

but, when I do that, if the connection is not there, then the program crashes. 
So, how could I check to see if the connection is there then break it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading from stdin first, then use curses

2013-09-01 Thread Antoon Pardon

Op 11-08-13 14:05, Timo Schmiade schreef:

Hi all,

I wrote a replacement for urlview to properly extract URLs from emails.
You can find the first draft here:

   https://github.com/the-isz/pyurlview

When I call it with an email file passed to the '-f' argument, it does
pretty much what I want already. However, I intend to use it in mutt,
which pipes the message to the program like so:

macro pager \cu 'pyurlview.py' 'Follow links with pyurlview'

The problem is rather obvious but - unfortunately - not so easy to solve:

* The program reads the mail from stdin
* The terminal in which it runs is a pseudo-terminal (pipe)
* curses is not able to accept user input from the pseudo-terminal

The question is:

How do I read from stdin first and afterwards allow curses to read user
input?



Well you could close fd 0. Then open /dev/tty en dup the fd to 0.
Then start curses.

--
Antoon Pardon

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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread anntzer . lee
On Sunday, September 1, 2013 4:37:34 AM UTC-7, Chris Angelico wrote:

> Yes, it most definitely CAN be a network config issue. The C function
> you want to be calling is getifaddrs(), and I don't think there's a
> way to call that from core Python. But a Google search for 'python
> getifaddrs' shows up a few third-party modules that might be of use to
> you; that'd be a lot quicker and more reliable than trying to look up
> your own hostname and depending on the results.
> 
> ChrisA

I tried using netifaces (https://pypi.python.org/pypi/netifaces) which seems to 
rely on getifaddrs (according to the doc, I didn't check the source).  Again, 
it returns nearly instantaneously the correct IP address.
-- 
http://mail.python.org/mailman/listinfo/python-list


MySQL data types vs Django/Python data types

2013-09-01 Thread Gary Roach

Hi all,

System:
Debian Wheezy Linux
Python 2.7
Django 1.5
MySql 5.5

I am new to Python and Django and am having trouble matching Python data 
types with those of MySQL. MySQL has about 7 basic data types including 
Blobs, Binaries, etc. It also has a rich selection of geometry types. In 
addition, types like INT have 7 or 8 different options like Primary Key, 
zero fill, auto inc, etc. I can't seem to find anything in python to 
match these. I am trying to build a model.py for an existing database 
that was created with MySQL Workbench.


I do not wish to use anything other than MySQL because of the complexity 
of my storage needs (searchable text, PDF, Audio, Video and Photos). The 
text searches will often be word phrase searches through thousands of 
documents. I need the speed and the data type flexibility.


How do I build or modify a model.py to do this. I really don't want to 
write a model.py manually. That would really be a major pain. Workbench 
to the database an import to Django and edit is my choice. 
Unfortunately, the model.py produced has lost most of the functionality 
of the original database and I can't seem to figure out how to fix it.


Any help will be sincerely appreciated.

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


Re: semicolon at end of python's statements

2013-09-01 Thread Antoon Pardon

Op 31-08-13 02:09, Steven D'Aprano schreef:

On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:





We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
 if predicate(spam):
 process(spam)





Adding a fourth option:

for spam in sequence if predicate(spam):
 process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.


So what is the big difference between this situation and the following:

| else:
| if condition:
| whatever

which in python we can write:

| elif condition:
| whatever


So either is seems this was a design mistake or a line and an indent
level can be important enough to allow a combination of controls.

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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread Chris Angelico
On Mon, Sep 2, 2013 at 6:37 AM,   wrote:
> On Sunday, September 1, 2013 4:37:34 AM UTC-7, Chris Angelico wrote:
>
>> Yes, it most definitely CAN be a network config issue. The C function
>> you want to be calling is getifaddrs(), and I don't think there's a
>> way to call that from core Python. But a Google search for 'python
>> getifaddrs' shows up a few third-party modules that might be of use to
>> you; that'd be a lot quicker and more reliable than trying to look up
>> your own hostname and depending on the results.
>>
>> ChrisA
>
> I tried using netifaces (https://pypi.python.org/pypi/netifaces) which seems 
> to rely on getifaddrs (according to the doc, I didn't check the source).  
> Again, it returns nearly instantaneously the correct IP address.

Perfect!

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 12:13 PM, Roy Smith wrote:

In article ,
  Ethan Furman  wrote:


On 09/01/2013 03:09 AM, Fabrice Pombet wrote:


So I guess that we are actually all agreeing on this one.


No, we are not.

"encapsulation" != "inaccessible except by getters/setters"


Nothing is accessible in Python except via getters and setters.  The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name :-)


class Javaesque:

__value = None

def get_value(self):
return self.__value

def set_value(self, new_value):
validate(new_value)
self.__value = new_value


class ProtectedPython:

_value = None

@property
def value(self):
return self._value

@value.setter
def value(self, new_value)
validate(new_value)
self._value = new_value

class PlainPython:

value = None


In the Javaesque class we see the unPythonic way of using getters/setters; in the ProtectedPython* class we see the 
pythonic way of providing getters/setters**; in the PlainPython class we have the standard, unprotected, direct access 
to the class attribute.


No where in PlainPython is a getter/setter defined, nor does Python define one 
for us behind our backs.

If you have evidence to the contrary I'd like to see it.


* Not the best name, but oh well.
** In Python, using @property makes getter/setter usage look just like normal 
attribute usage, which is cool.

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Tim Delaney
On 2 September 2013 06:33, Ethan Furman  wrote:

>
> class PlainPython:
>
> value = None
>
>
> In the Javaesque class we see the unPythonic way of using getters/setters;
> in the ProtectedPython* class we see the pythonic way of providing
> getters/setters**; in the PlainPython class we have the standard,
> unprotected, direct access to the class attribute.
>
> No where in PlainPython is a getter/setter defined, nor does Python define
> one for us behind our backs.
>
> If you have evidence to the contrary I'd like to see it.
>

I think Roy is referring to the fact that attribute access is implemented
via __getattr__ / __getattribute__ / __setattr__ / __delattr__. From one
point of view, he's absolutely correct - nearly all attributes are accessed
via getters/setters in Python.

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article ,
 Tim Delaney  wrote:

> On 2 September 2013 06:33, Ethan Furman  wrote:
> 
> >
> > class PlainPython:
> >
> > value = None
> >
> >
> > In the Javaesque class we see the unPythonic way of using getters/setters;
> > in the ProtectedPython* class we see the pythonic way of providing
> > getters/setters**; in the PlainPython class we have the standard,
> > unprotected, direct access to the class attribute.
> >
> > No where in PlainPython is a getter/setter defined, nor does Python define
> > one for us behind our backs.
> >
> > If you have evidence to the contrary I'd like to see it.
> >
> 
> I think Roy is referring to the fact that attribute access is implemented
> via __getattr__ / __getattribute__ / __setattr__ / __delattr__. From one
> point of view, he's absolutely correct - nearly all attributes are accessed
> via getters/setters in Python.
> 
> Tim Delaney

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 1/9/2013 7:10 μμ, ο/η Ferrous Cranus έγραψε:

Στις 1/9/2013 6:36 μμ, ο/η Dave Angel έγραψε:

On 1/9/2013 10:08, Ferrous Cranus wrote:



Here is it:


errout = open( '/tmp/err.out', 'w' )# opens and truncates the
error
output file
try:
gi = pygeoip.GeoIP('/usr/local/share/GeoIPCity.dat')
city = gi.time_zone_by_addr( os.environ['REMOTE_ADDR'] ) or
gi.time_zone_by_addr( os.environ['HTTP_CF_CONNECTING_IP'] )
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] or
socket.gethostbyaddr( os.environ['HTTP_CF_CONNECTING_IP'] )[0] or "Proxy
Detected"
except Exception as e:
print( "Xyzzy exception-", repr( sys.exc_info() ), file=errout )
  errout.flush()

sys.exit(0)

and the output of error file is:


[email protected] [~]# cat /tmp/err.out
UnicodeDecodeError('utf-8', b'\xb6\xe3\xed\xf9\xf3\xf4\xef
\xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2', 0, 1,
'invalid start byte')



Nope.  The label  "Xyzzy exception" is not in that file, so that's not
the file you created in this run.  Further, if that line existed before,
it would have been wiped out by the open with mode "w".

i suggest you add yet another write to that file, immediately after
opening it:

errout = open( '/tmp/err.out', 'w' )# opens and truncates the
error
print("starting run", file=errorout)
errout.flush()

Until you can reliably examine the same file that was logging your
errors, you're just spinning your wheels.  you might even want to write
the time to the file, so that you can tell whether it was now, or 2 days
ago that the run was made.





I tried it and it printed nothing.
But suddenly thw ebpage sttaed to run and i get n invalid byte entried
and no weird messge files.py is working as expcted.
what on earht?

Now i ahve thso error:

#
=

# DATABASE INSERTS - do not increment the counter if a Cookie is set to
the visitors browser already
#
=

if( not vip and re.search(
r'(msn|gator|amazon|yandex|reverse|cloudflare|who|fetch|barracuda|spider|google|crawl|pingdom)',
host ) is None ):

 print( "i'm in and data is: ", host )
 try:
 #find the needed counter for the page URL
 if os.path.exists( path + page ) or os.path.exists( cgi_path +
page ):
 cur.execute('''SELECT ID FROM counters WHERE url = %s''',
page )
 data = cur.fetchone()#URL is unique, so should only
be one

 if not data:
 #first time for page; primary key is automatic, hit is
defaulted
 cur.execute('''INSERT INTO counters (url) VALUES (%s)''',
page )
 cID = cur.lastrowid#get the primary key value of
the new record
 else:
 #found the page, save primary key and use it to issue hit
UPDATE
 cID = data[0]
 cur.execute('''UPDATE counters SET hits = hits + 1 WHERE ID
= %s''', cID )

 #find the visitor record for the (saved) cID and current host
 cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
host = %s''', (cID, host) )
 data = cur.fetchone()#cID&host are unique

 if not data:
 #first time for this host on this page, create new record
 cur.execute('''INSERT INTO visitors (counterID, host, city,
useros, browser, lastvisit) VALUES (%s, %s, %s, %s, %s, %s)''', (cID,
host, city, useros, browser, date) )
 else:
 #found the page, save its primary key for later use
 vID = data[0]
 #UPDATE record using retrieved vID
 cur.execute('''UPDATE visitors SET city = %s, useros = %s,
browser = %s, hits = hits + 1, lastvisit = %s
 WHERE counterID = %s and host =
%s''', (city, useros, browser, date, vID, host) )

 con.commit()#if we made it here, the transaction is
complete

 except pymysql.ProgrammingError as e:
 print( repr(e) )
 con.rollback()#something failed, rollback the entire
transaction
 sys.exit(0)


i get no counter increment when visitors visit my webpage.
What on eart is going on?

How the previous error with the invalid byte somehtign got solved?


i still wonder how come the invalid byte messge dissapeared

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


Re: UnicodeDecodeError issue

2013-09-01 Thread Dave Angel
On 1/9/2013 18:23, Ferrous Cranus wrote:


>>
> i still wonder how come the invalid byte messge dissapeared
>

Too bad you never bothered to narrow it down to its source.  It could
be anywhere on those three lines.  If I had to guess, I'd figure it was
one of those environment variables.  The Linux environment variables are
strings of bytes, and the os.environ is a dict of strings.  Apparently
it converts them using utf-8, and if you've somehow set them using some
other encoding, you could be getting that error.

Have you tried to decode those bytes in various encodings other than
utf-8 ?

-- 
Signature file not found

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


Re: semicolon at end of python's statements

2013-09-01 Thread MRAB

On 01/09/2013 20:58, Antoon Pardon wrote:

Op 31-08-13 02:09, Steven D'Aprano schreef:

On Fri, 30 Aug 2013 11:32:17 +0100, Fábio Santos wrote:





We really are spoiled for choice here. We can write any of these:

# Option 1
for spam in sequence:
 if predicate(spam):
 process(spam)





Adding a fourth option:

for spam in sequence if predicate(spam):
 process(spam)

saves absolutely nothing except a line and an indent level, neither of
which are in short supply, and gains nothing in readability over Option 1.


So what is the big difference between this situation and the following:

| else:
| if condition:
| whatever

which in python we can write:

| elif condition:
| whatever


So either is seems this was a design mistake or a line and an indent
level can be important enough to allow a combination of controls.


'elif' is for cascading ifs:

if ...:
   ...
elif ...:
   ...
elif ...:
   ...
elif ...:
   ...
else:
   ...

Without 'elif' you would be forced to write:

if ...:
   ...
else:
if ...:
   ...
else:
if ...:
   ...
else:
if ...:
   ...
else:
   ...

On the other hand, for...if is much less of a problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Ben Finney
Steven D'Aprano  writes:

> […] programmers have been using the *principle* of encapsulation since
> before Grace Hopper was an admiral.

[…]
> Encapsulation and information hiding are distinct things -- you can
> have one without the other.

[…]
> One of the most obnoxious and annoying traits of OOP zealots,
> especially academics, is that they act as if programming did not exist
> before Java and C++, or if you're really lucky, Smalltalk. (Somehow
> they nearly always forget about Simula.)

Yes. That's something which has been pointed out to such people, even in
the Java community, for most (all?) of Java's history.

Here's a JavaWorld article from 2001, by a Java programmer, with the
clear title “Encapsulation is not information hiding”:

Encapsulation refers to the bundling of data with the methods that
operate on that data. Often that definition is misconstrued to mean
that the data is somehow hidden.


http://www.javaworld.com/javaworld/jw-05-2001/jw-0518-encapsulation.html>

(That site unfortunately slices up the article into many pages to
increase advertising hits and reader frustration, my apologies.)


One of the more annoying traits of humanity is that whatever context we
first encounter a term is disproportionately privileged, causing us to
irrationally dismiss better (more useful, more reasonable, more
pedagogically appropriate, more historically correct, etc.) definitions.

This thread is an excellent illustration that the field of programming
is no exception.

-- 
 \   Moriarty: “Forty thousand million billion dollars? That money |
  `\must be worth a fortune!” —The Goon Show, _The Sale of |
_o__)   Manhattan_ |
Ben Finney

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


How to split with "\" character, and licence copyleft mirror of ©

2013-09-01 Thread materile11
Hello everybody
I'm trying to run this: 


>>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
>>> a.split('\')

SyntaxError: EOL while scanning string literal


I think that the character '\' is the problem, but unfortunately I'm developing 
a small app for windows and I need to show only the name of the .wav file, in 
this case 'flute.wav'.

I also want to know how to mirror a character, in my case this one ©, because 
I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft to distribute my 
app.

Thanks.

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


Re: How to split with "\" character, and licence copyleft mirror of ©

2013-09-01 Thread Cameron Simpson
On 01Sep2013 17:03, [email protected]  wrote:
| 
| >>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
| >>> a.split('\')
| SyntaxError: EOL while scanning string literal
| 
| 
| I think that the character '\' is the problem, but unfortunately I'm 
developing a small app for windows and I need to show only the name of the .wav 
file, in this case 'flute.wav'.

Firstly, you want to say '\\' for a slosh (just as you would say '\n' for a 
linefeed).

However, you really should use the os.path module, in particular 
os.path.split().

Have a read of:
  http://docs.python.org/3/library/os.path.html#module-os.path
  http://docs.python.org/2/library/os.path.html#module-os.path

| I also want to know how to mirror a character, in my case this
| one ©, because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
| to distribute my app.

Isn't that a copyright symbol? I'd have a look at the "uncidoedata" module,
myself.

Cheers,
-- 
Cameron Simpson 

Just because Unix is a multiuser system doesn't mean I want to share it with
anybody!- Paul Tomblin, in rec.aviation.military
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to split with "\" character, and licence copyleft mirror of ©

2013-09-01 Thread Tim Chase
On 2013-09-01 17:03, [email protected] wrote:
> Hello everybody
> I'm trying to run this: 
> 
> 
> >>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
> >>> a.split('\')
> 
> SyntaxError: EOL while scanning string literal
> 
> 
> I think that the character '\' is the problem, but unfortunately
> I'm developing a small app for windows and I need to show only the
> name of the .wav file, in this case 'flute.wav'.

To directly answer your question, you need to escape the "\" so it's

  a.split('\\')

That said, it's far better to use Python's built-ins to do the
processing for you:

  >>> import os
  >>> print os.path.basename(a)
  flute.wav

which does what you want *and* works cross-platform:

  [on Linux]
  >>> a = '/home/tkc/path/to/flute.wav'
  >>> print os.path.basename(a)
  flute.wav

> I also want to know how to mirror a character, in my case this one
> ©, because I'll use the Copyleft

This can't be done in much of a general way:  Unicode doesn't specify
this character, and the URL you provided suggests combining two
Unicode characters to get ↄ⃝  Unfortunately, (1) it requires a
display that knows how to produce that, which many terminals can't;
and (2) it's purely visual, not semantic.  If that's what you really
want, you should be able to use:

  copyleft_symbol = u"\u2184\u20DD"

Just be aware that it may not always display the way you expect it to.

-tkc



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


Re: How to split with "\" character, and licence copyleft mirror of ©

2013-09-01 Thread Tim Chase
On 2013-09-02 10:23, Cameron Simpson wrote:
> | I also want to know how to mirror a character, in my case this
> | one ©, because I'll use the Copyleft
> http://en.wikipedia.org/wiki/Copyleft | to distribute my app.
> 
> Isn't that a copyright symbol? I'd have a look at the "uncidoedata"
> module, myself.

Thanks to his link (which would have been more helpful with the
URL fragment:

http://en.wikipedia.org/wiki/Copyleft#Symbol

), I suspect the he means that it should be "the mirror image of a
copyright symbol".

And that would be "unicodedata", not "uncidoedata" (I don't think
that was the reversing he was talking about ;-)

-tkc



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


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 02:54 PM, Tim Delaney wrote:

Roy Smith wrote:


Nothing is accessible in Python except via getters and setters.  The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name


I think Roy is referring to the fact that attribute access is implemented via 
__getattr__ / __getattribute__ /
__setattr__ / __delattr__. From one point of view, he's absolutely correct - 
nearly all attributes are accessed via
getters/setters in Python.


Seems to me there is a difference between an underlying generic protocol for data manipulation and "Python writing them 
[getters/setters] for you".


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


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article ,
 Ben Finney  wrote:

> One of the more annoying traits of humanity is that whatever context we
> first encounter a term is disproportionately privileged, causing us to
> irrationally dismiss better (more useful, more reasonable, more
> pedagogically appropriate, more historically correct, etc.) definitions.

Known in the education world as "The law of primacy".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Roy Smith
In article ,
 Ethan Furman  wrote:

> On 09/01/2013 02:54 PM, Tim Delaney wrote:
> > Roy Smith wrote:
> >>
> >> Nothing is accessible in Python except via getters and setters.  The
> >> only difference between Python and, say, C++ in this regard is that the
> >> Python compiler writes them for you most of the time and doesn't make
> >> you put ()'s at the end of the name
> >
> > I think Roy is referring to the fact that attribute access is implemented 
> > via __getattr__ / __getattribute__ /
> > __setattr__ / __delattr__. From one point of view, he's absolutely correct 
> > - nearly all attributes are accessed via
> > getters/setters in Python.
> 
> Seems to me there is a difference between an underlying generic protocol for 
> data manipulation and "Python writing them 
> [getters/setters] for you".

Why?  When I write "foo.bar", a bunch of generic code gets run which 
figures out what value to return.  If I don't like the generic behavior, 
I can write my own __getattrr__(), etc, and make it do whatever I want.

How is that any different from, in C++, if you don't write a default 
constructor, the compiler will write one for you.  If you don't like the 
generic behavior you get from that, you can write your own and make it 
do whatever you want.
-- 
http://mail.python.org/mailman/listinfo/python-list


Simplex Algorithm

2013-09-01 Thread Tommy Vee
Anyone know where I can get an easy to use Python class or algorithm for 
the Simplex optimization algorithm?  I've tried the one in the link 
below, but I can't figure out if a) I'm using it properly, or b) where 
to get the solution.  BTW, I tried some test scenarios using MS Excel's 
"Solver" and just can't get this algorithm to match Excel's results 
(which is spot on).


http://taw9.hubpages.com/hub/Simplex-Algorithm-in-Python

BTW, if I can't something to work, I'm going to be forced to use the VBA 
programmatic Excel interface. That wouldn't be too bad, but the data 
comes from a DB and getting it properly positioned to use Excel's 
"Solver" is very painful.  A Python approach would be much cleaner.


Thanks,

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


Re: Encapsulation unpythonic?

2013-09-01 Thread Steven D'Aprano
On Sun, 01 Sep 2013 15:13:06 -0400, Roy Smith wrote:

> In article ,
>  Ethan Furman  wrote:
> 
>> On 09/01/2013 03:09 AM, Fabrice Pombet wrote:
>> >
>> > So I guess that we are actually all agreeing on this one.
>> 
>> No, we are not.
>> 
>> "encapsulation" != "inaccessible except by getters/setters"
> 
> Nothing is accessible in Python except via getters and setters.  The
> only difference between Python and, say, C++ in this regard is that the
> Python compiler writes them for you most of the time and doesn't make
> you put ()'s at the end of the name :-)

Very clever! Pedantic, and an unusual look at what's going on under the 
hood!

I wanted to say it was *not quite correct*, because you can read or write 
directly to the instance dict:

instance.__dict__['name'] = 42


If I understand Python's internals correctly, __dict__ is a slot, and so 
bypasses the usual getattr machinary. But even if so, __dict__['name'] 
uses the dictionary __get/setitem__ method, so it's still a getter/setter 
under the hood.

In any case, even if you are *technically* correct that Python has 
getters and setters under the hood, that's not quite what the discussion 
here is about. But I'm sure you realise that :-)


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


Re: How to split with "\" character, and licence copyleft mirror of �

2013-09-01 Thread Tim Roberts
[email protected] wrote:

>Hello everybody
>I'm trying to run this: 
>
>
 a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
 a.split('\')
>
>SyntaxError: EOL while scanning string literal
>
>
>I think that the character '\' is the problem, but unfortunately I'm
>developing a small app for windows and I need to show only the name
>of the .wav file, in this case 'flute.wav'.

I assume you know that backslash has a special meaning in string constants.
For example the string '\n\r' contains exactly two characters, and no
backslashes.

When you want to use an actual backslash in an ordinary string constant,
you have to double it.  So, you could have written your code as:
a = 'E:\\Dropbox\\jjfsdjjsdklfj\\sdfjksdfkjslkj\\flute.wav'
a.split('\\')

Another altrnative is to use "raw" strings, in which backslashes are not
interpreted:
a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
a.split(r'\')

I assume your filename is actually input to your program, and not as a
constant in your code, so that may not be a problem.  However, there is an
API to do exactly what you're asking:

  >>> import os
  >>> a=r'E:\Dropbox\one\two\three\flute.wav'
  >>> os.path.split(a)
  ('E:\\Dropbox\\one\\two\\three', 'flute.wav')
  >>> os.path.split(a)[1]
  'flute.wav'
  >>>
  
>I also want to know how to mirror a character, in my case this one ©,
>because I'll use the Copyleft http://en.wikipedia.org/wiki/Copyleft
>to distribute my app.

You can't "mirror" a character.  That is an invented glyph that is not
present in Unicode.  Fortunately, the character doesn't have any legal
meaning, so you can just include explanatory text in your description that
identifies your license.
-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Steven D'Aprano
On Sun, 01 Sep 2013 20:59:25 -0400, Roy Smith wrote:

> In article ,
>  Ethan Furman  wrote:
> 
>> On 09/01/2013 02:54 PM, Tim Delaney wrote:
>> > Roy Smith wrote:
>> >>
>> >> Nothing is accessible in Python except via getters and setters.  The
>> >> only difference between Python and, say, C++ in this regard is that
>> >> the Python compiler writes them for you most of the time and doesn't
>> >> make you put ()'s at the end of the name
>> >
>> > I think Roy is referring to the fact that attribute access is
>> > implemented via __getattr__ / __getattribute__ /
>> > __setattr__ / __delattr__. From one point of view, he's absolutely
>> > correct - nearly all attributes are accessed via getters/setters in
>> > Python.
>> 
>> Seems to me there is a difference between an underlying generic
>> protocol for data manipulation and "Python writing them
>> [getters/setters] for you".
> 
> Why?  When I write "foo.bar", a bunch of generic code gets run which
> figures out what value to return.  If I don't like the generic behavior,
> I can write my own __getattrr__(), etc, and make it do whatever I want.
> 
> How is that any different from, in C++, if you don't write a default
> constructor, the compiler will write one for you.  If you don't like the
> generic behavior you get from that, you can write your own and make it
> do whatever you want.


And fundamentally, all programming is flipping bits, therefore all 
languages are exactly the same, right? :-)


I can't speak for C++, but comparing Java and Python there are 
differences:


- Java the language makes it a pain to change your mind and convert a 
public attribute to a computed attribute. Since the pain of changing your 
mind far outweighs the pain of writing trivial getters/setters up front, 
it is good defensive practice to make attribute access via getters just 
in case. Python makes it trivial to change your mind, and so YAGNI rules 
and you shouldn't write getters unless you actually need them.

- Perhaps because the focus in Java is on massive projects with large 
numbers of barely adequate coders, Java tries to protect the average 
coder from shooting themselves in the foot. Consequently, Java encourages 
a philosophy of "default deny" when it comes to attribute access: don't 
give your caller access to anything except the absolute minimum you know 
they need. In Python, the focus tends to be more about smaller projects 
with small teams of better than average coders, and a philosophy of 
"we're all adults here". Consequently, Python code tends towards 
"everything not explicitly prohibited is permitted".

- Similarly, while Java the language doesn't force you to *explicitly* 
declare members as public (if you don't declare members private, they are 
public), it strongly encourages you to think about information hiding and 
explicitly mark members as public. Python does not. There is no way to 
explicitly mark attributes as public.

- The Java compiler enforces public/private, while Python treats it as a 
convention. (At least for pure-Python code.)


But notice that they are differences of degree, not kind. Java encourages 
information hiding in classes, but does not prohibit you from making 
members public, and using reflection you can break into classes and get 
access to anything; Python allows information hiding, but trusts the 
programmer to honour "private" names, and reflection is such a 
fundamental part of Python that we don't even call it that.


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


Re: How to split with "\" character, and licence copyleft mirror of (c)

2013-09-01 Thread Zero Piraeus
:

On 1 September 2013 22:40, Tim Roberts  wrote:
> Another altrnative is to use "raw" strings, in which backslashes are not
> interpreted:
> a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
> a.split(r'\')

Acually, that doesn't work:

>>> a = r'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
>>> a.split('\')
  File "", line 1
a.split('\')
   ^
SyntaxError: EOL while scanning string literal

... because you can't end a raw string with a backslash:

http://docs.python.org/3/faq/design.html#why-can-t-raw-strings-r-strings-end-with-a-backslash

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError issue

2013-09-01 Thread Ferrous Cranus

Στις 2/9/2013 2:14 πμ, ο/η Dave Angel έγραψε:

On 1/9/2013 18:23, Ferrous Cranus wrote:

 



i still wonder how come the invalid byte messge dissapeared



Too bad you never bothered to narrow it down to its source.



if only i knew how up until yesterday when they were appearing.



It could
be anywhere on those three lines.  If I had to guess, I'd figure it was
one of those environment variables.  The Linux environment variables are
strings of bytes, and the os.environ is a dict of strings.  Apparently
it converts them using utf-8, and if you've somehow set them using some
other encoding, you could be getting that error.

Have you tried to decode those bytes in various encodings other than
utf-8 ?



No, because i wasn't aware of what string/variable they were pertaining at.


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


Re: How to split with "\" character, and licence copyleft mirror of ©

2013-09-01 Thread materile11
El domingo, 1 de septiembre de 2013 19:34:16 UTC-5, Tim Chase  escribió:
> On 2013-09-01 17:03, [email protected] wrote:
> 
> > Hello everybody
> 
> > I'm trying to run this: 
> 
> > 
> 
> > 
> 
> > >>> a = 'E:\Dropbox\jjfsdjjsdklfj\sdfjksdfkjslkj\flute.wav'
> 
> > >>> a.split('\')
> 
> > 
> 
> > SyntaxError: EOL while scanning string literal
> 
> > 
> 
> > 
> 
> > I think that the character '\' is the problem, but unfortunately
> 
> > I'm developing a small app for windows and I need to show only the
> 
> > name of the .wav file, in this case 'flute.wav'.
> 
> 
> 
> To directly answer your question, you need to escape the "\" so it's
> 
> 
> 
>   a.split('\\')
> 
> 
> 
> That said, it's far better to use Python's built-ins to do the
> 
> processing for you:
> 
> 
> 
>   >>> import os
> 
>   >>> print os.path.basename(a)
> 
>   flute.wav
> 
> 
> 
> which does what you want *and* works cross-platform:
> 
> 
> 
>   [on Linux]
> 
>   >>> a = '/home/tkc/path/to/flute.wav'
> 
>   >>> print os.path.basename(a)
> 
>   flute.wav
> 
> 
> 
> > I also want to know how to mirror a character, in my case this one
> 
> > ©, because I'll use the Copyleft
> 
> 
> 
> This can't be done in much of a general way:  Unicode doesn't specify
> 
> this character, and the URL you provided suggests combining two
> 
> Unicode characters to get ↄ⃝  Unfortunately, (1) it requires a
> 
> display that knows how to produce that, which many terminals can't;
> 
> and (2) it's purely visual, not semantic.  If that's what you really
> 
> want, you should be able to use:
> 
> 
> 
>   copyleft_symbol = u"\u2184\u20DD"
> 
> 
> 
> Just be aware that it may not always display the way you expect it to.
> 
> 
> 
> -tkc


Thank you, I've used the os.path.basename to solve my problem.
Regards.
-- 
http://mail.python.org/mailman/listinfo/python-list


A Pragmatic Case for Static Typing

2013-09-01 Thread Russ P.
I just stumbled across this video and found it interesting:

http://vimeo.com/72870631

My apologies if it has been posted here already.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encapsulation unpythonic?

2013-09-01 Thread Ethan Furman

On 09/01/2013 05:59 PM, Roy Smith wrote:

  Ethan Furman wrote:

On 09/01/2013 02:54 PM, Tim Delaney wrote:

Roy Smith wrote:


Nothing is accessible in Python except via getters and setters.  The
only difference between Python and, say, C++ in this regard is that the
Python compiler writes them for you most of the time and doesn't make
you put ()'s at the end of the name


I think Roy is referring to the fact that attribute access is implemented
via __getattr__ / __getattribute__ /
__setattr__ / __delattr__. From one point of view, he's absolutely correct
- nearly all attributes are accessed via
getters/setters in Python.


Seems to me there is a difference between an underlying generic protocol for
data manipulation and "Python writing them
[getters/setters] for you".


Why?  When I write "foo.bar", a bunch of generic code gets run which
figures out what value to return.  If I don't like the generic behavior,
I can write my own __getattrr__(), etc, and make it do whatever I want.


I don't yet know C++ so I can't speak to it.

In Python if you are writing your own __getattr__, etc., you are writing the underlying protocol itself.  To write a 
getter/setter you would just use


   def get_soemthing(...)
   def set_something(...)

or, even better

   @property
   def something(...)

Maybe, as Steven said, it's just a matter of degree:  my understanding of getters/setters is that they are per 
attribute, while __getattr__ will be called for every attribute not otherwise found, __setattr__ will be called for 
every assignment, __delattr__ will be called for every deletion, and all that is assuming you haven't written your own 
__getattribute__ and completely changed the rules for your class.


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


Re: gethostbyname_ex(hostname) extremely slow (crossposted from stackoverflow)

2013-09-01 Thread anntzer . lee
On Sunday, September 1, 2013 2:03:56 PM UTC-7, Chris Angelico wrote:

> > I tried using netifaces (https://pypi.python.org/pypi/netifaces) which 
> > seems to rely on getifaddrs (according to the doc, I didn't check the 
> > source).  Again, it returns nearly instantaneously the correct IP address.
> 
> Perfect!
> 
> ChrisA

Not really for my use case -- it isn't that *I* want to know my public IP 
address, but rather that IPython wants to know it.  Of course I could patch 
IPython's source to use netifaces but that sounds like an overkill.

As it happens I found a better way: just add the proper entry to /etc/hosts.

Still, thanks for the suggestions.

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