Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread nagia . retsina
Τη Κυριακή, 9 Ιουνίου 2013 3:31:44 μ.μ. UTC+3, ο χρήστης Steven D'Aprano έγραψε:

> py> c = 'α'
> py> ord(c)
> 945

The number 945 is the characters 'α' ordinal value in the unicode charset 
correct?

The command in the python interactive session to show me how many bytes
this character will take upon encoding to utf-8 is:

>>> s = 'α'
>>> s.encode('utf-8')
b'\xce\xb1'

I see that the encoding of this char takes 2 bytes. But why two exactly?
How do i calculate how many bits are needed to store this char into bytes?


Trying to to the same here but it gave me no bytes back.

>>> s = 'a'
>>> s.encode('utf-8')
b'a'


>py> c.encode('utf-8')
> b'\xce\xb1'

2 bytes here. why 2?

> py> c.encode('utf-16be')
> b'\x03\xb1'

2 byets here also. but why 3 different bytes? the ordinal value of char 'a' is 
the same in unicode. the encodign system just takes the ordinal value end 
encode, but sinc eit uses 2 bytes should these 2 bytes be the same?

> py> c.encode('utf-32be')
> b'\x00\x00\x03\xb1

every char here takes exactly 4 bytes to be stored. okey.

> py> c.encode('iso-8859-7')
> b'\xe1'

And also does '\x' means that the value is being respresented in hex way?
and when i bin(6) i see '0b101'

I should expect to see 8 bits of 1s and 0's. what the 'b' is tryign to say?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re-using copyrighted code

2013-06-10 Thread Chris Angelico
On Mon, Jun 10, 2013 at 4:42 PM, Malte Forkel  wrote:
> Had I known in the beginning how convoluted things would become, I might
> have considered two other options: Just publish or keep the code to
> myself.  But I still think, first understanding the legal aspects and
> then publishing (to give back at least a little) is the way to go.
>
> I certainly hope that there is no software out thre that didn't get
> released to the public because its author found the legal implications
> to difficult to handle.

Understanding is good :)

Unfortunately there will be heaps of software that didn't get released
owing to potential legal messes. It's a loss that could be partially
avoided in future by authors sticking to the well-known licenses; it's
easy to make a derivative work based on three or four different
components if they all use the same license. I've seen issues all over
the place stemming from GNU Readline (which is GPL, not LGPL) and
something not GPL-compat being both linked to the same application;
and it's so likely as to be practically certain that there have been
things left unreleased because of that.

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


Re: Re-using copyrighted code

2013-06-10 Thread Mark Janssen
>>> Can you provide any citations for your interpretation? Besides "that's
>>> what the law should be", I mean.
>>
>> I don't think I even have to:  the legal code you're citing above is
>> not very clear, consistent, or well-defined at all.  As such, it shows
>> that this area remains an area that has yet to be worked out by all
>> parties involved.   I would happily volunteer for any interested
>> parties to such a broken system.  Alternatively, I've been working on
>> a real fix to IP protections in the form of a unified data model for
>> the internet and data ecosystem.
>
> Except that's now how law works in the US.  All laws are unclear,
> inconsistent or ill-defined.

Yes, but the issue is that some participants were suggesting that the
law *is* clear when it is not -- so what is the procedure to follow
when that is the case?

>Many laws even contradict existing laws.
> That's why there's a long history and tradition (for good or ill) of
> courts establishing case law to clarify and codify the implementation of
> law, and to resolve incompatibilities and consistencies.
>
> So while your views may be logical to you, and even common sense, unless
> case law backs you up, your opinions are irrelevant to the actual
> implementation of copyright law.

No, the system of law is made for, and by the people, so while it may
not reflect consensus, it isn't necessarily irrelevant and in any case
where there are people spouting law as if it WAS clear, someone must
do the job breaking down the walls.

> As much as many of us are open source or even free software advocates,
> we do have to live within the copyright law currently,

We do not have to live within it, we can create it.  Where did you get
this idea?

-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with if then

2013-06-10 Thread Jean Dubois
On 9 jun, 23:35, Roy Smith  wrote:
> In article
> <20165c85-4cc3-4b79-943b-82443e4a9...@w7g2000vbw.googlegroups.com>,
>  Jean Dubois  wrote:
>
> > But, really,
> > > once you've done all that (and it's worth doing as an exercise), rewrite
> > > your code to use urllib2 or requests.  It'll be a lot easier.
>
> > Could you show me how to code the  example in metacode below wuth the
> > use of urllib2?
> > #!/usr/bin/env python
> > import urllib2
> > if check whether url exists succeed:
> >     print 'url exists'
> > else:
> >     print 'url does not exist'
>
> There are two basic ways Python function return status information.
> Either they return something which indicates failure (such as None), or
> they raise an exception.
>
> In this case, what you want is urlopen(), which is documented 
> athttp://docs.python.org/2/library/urllib2.html.  The key piece of
> information is a couple of paragraphs down, where it says, "Raises
> URLError on errors".
>
> It's not obvious from reading that whether URLError is a built-in or
> not, but that's easy enough to figure out:
>
> >>> URLError
>
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'URLError' is not defined>>> import urllib2
> >>> urllib2.URLError
>
> 
>
> So, that means you want something like:
>
> #!/usr/bin/env python
>
> import urllib2
>
> url = "http://whatever";
> try:
>    urllib2.urlopen(url)
>    print "url exists"
> except urllib2.URLError:
>    print "url does not exist"

thanks a lot, this works like a charm

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


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Larry Hudson

On 06/09/2013 03:37 AM, Νικόλαος Κούρας wrote:



I mean utf-8 could use 1 byte for storing the 1st 256 characters. I meant up to 
256, not above 256.


NO!!

0 - 127, yes.
128 - 255 -> one byte of a multibyte code.

That's why the decode fails, it sees it as incomplete data so it can't do 
anything with it.



A surrogate pair is like itting for example Ctrl-A, which means is a 
combination character that consists of 2 different characters?
Is this what a surrogate is? a pari of 2 chars?

You're confusing character encodings with the way NON-CHARACTER keys on the KEYBOARD are encoded 
(function keys, arrow keys and such).  These are NOT text characters but KEYBOARD key codes. 
These are NOT text codes and are entirely different and not related to any character encoding. 
How programs interpret and use these codes depends entirely on the individual programs.  There 
are common conventions on how many are used, but there are no standards.


Also the control-codes are the first 32 values of the ASCII (and ASCII-compatible) character set 
and are not multi-character key codes like the keyboard non-character keys.


However, there are a few keyboard keys that actually produce control-codes.  A 
few examples:

Return/Enter -> Ctrl-M
Tab -> Ctrl-I
Backspace -> Ctrl-H



So character 'A' <-> 65 (in decimal uses in charset's table)  <-> 01011100 (as binary 
stored in disk) <-> 0xEF (as hex, when we open the file with a hex editor)

You are trying to put too much meaning to this.  The value stored on disk, in memory, or 
whatever is binary bits, nothing else.  How you describe the value, in decimal, in octal, in 
hex, in base-12, or... is totally irrelevant.  These are simply different ways of describing or 
naming these numeric values.


It's the same as saying 3 in English is three, in Spanish is tres, in German is drei...  (I 
don't know Greek, sorry.)  No matter what you call it, it is still the numeric integer value 
that is between 2 and 4.


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


Re: Redirecting to a third party site with injected HTML

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 07:58, "Guy Tamir"  wrote:
> since i wrote some extensions in the past that allowed me to change the
DOM easily i thought there might be an way to do so from the server before
redirecting the user..

There could be a way to do that, but some of the features in the target
site would not be working. You could load the site yourself on your server,
and modify and serve that HTML to the user. Then you'd have to take care to
rewrite or disable all the links on the page (they may be relative to the
target domain and as such mean nothing on yours). Also images and other
resources' URLs have to be rewritten if they are relative URLs.

Then there are legal problems. Make sure you are legally allowed to do what
you are trying to do. I can't help with that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Redirecting to a third party site with injected HTML

2013-06-10 Thread Denis McMahon
On Sun, 09 Jun 2013 10:09:17 -0700, guytamir1 wrote:

> i'm not really sure how to approach this problem..
> hints :)

Let me restate the problem for you:

You want to display a web page to a visitor that exists on a third party 
website, with some of your own html inserted into it.

Setting aside the multitude of ethical, moral, legal and copyright 
issues, the only technical solution I can see that doesn't involve 
hacking the third party website is to scrape the third party website 
using eg curl, modify the html using your scripting environment of choice 
(I'll assume python) either using some form of dom manipulation or string 
manipulation, and then server the modified page to the visitor.

so pycurl and pydom might be good places to start.

Don't forget that you may need to rewrite urls in the scraped document 
for things such as anchors, images, css, javascript etc to point them 
back at the host server, or some script on your server that can obtain 
and serve the appropriate resources.

Alternatively, how about displaying the third party website in an iframe 
within your own document? Although that's not really pythonic, just 
htmlic.

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


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 10:51:34 π.μ. UTC+3, ο χρήστης Larry Hudson έγραψε:

> > I mean utf-8 could use 1 byte for storing the 1st 256 characters. I meant 
> > up to 256, not above 256.

> 0 - 127, yes.
> 128 - 255 -> one byte of a multibyte code.

you mean that in utf-8 for 1 character to be stored, we need 2 bytes?
I still havign troubl e understanding this.

Since 2^8 = 256, utf-8 would need 1 byte to store the 1st 256 characters but 
instead its using 1 byte up to the first 127 value and then 2 bytes for 
anyhtign above.  Why?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Andreas Perstinger

On 10.06.2013 09:10, nagia.rets...@gmail.com wrote:

Τη Κυριακή, 9 Ιουνίου 2013 3:31:44 μ.μ. UTC+3, ο χρήστης Steven D'Aprano έγραψε:


py> c = 'α'
py> ord(c)
945


The number 945 is the characters 'α' ordinal value in the unicode charset 
correct?


Yes, the unicode character set is just a big list of characters. The 
946th character in that list (starting from 0) happens to be 'α'.



The command in the python interactive session to show me how many bytes
this character will take upon encoding to utf-8 is:


s = 'α'
s.encode('utf-8')

b'\xce\xb1'

I see that the encoding of this char takes 2 bytes. But why two exactly?


That's how the encoding is designed. Haven't you read the wikipedia 
article which was already mentioned several times?



How do i calculate how many bits are needed to store this char into bytes?


You need to understand how UTF-8 works. Read the wikipedia article.


Trying to to the same here but it gave me no bytes back.


s = 'a'
s.encode('utf-8')

b'a'


The encode method returns a byte object. It's length will tell you how 
many bytes there are:


>>> len(b'a')
1
>>> len(b'\xce\xb1')
2

The python interpreter will represent all values below 256 as ASCII 
characters if they are printable:


>>> ord(b'a')
97
>>> hex(97)
'0x61'
>>> b'\x61' == b'a'
True

The Python designers have decided to use b'a' instead of b'\x61'.


py> c.encode('utf-8')
b'\xce\xb1'


2 bytes here. why 2?


Same as your first question.


py> c.encode('utf-16be')
b'\x03\xb1'


2 byets here also. but why 3 different bytes? the ordinal value of
char 'a' is the same in unicode. the encodign system just takes the
ordinal value end encode, but sinc eit uses 2 bytes should these 2 bytes
be the same?


'utf-16be' is a different encoding scheme, thus it uses other rules to 
determine how each character is translated into a byte sequence.



py> c.encode('iso-8859-7')
b'\xe1'


And also does '\x' means that the value is being respresented in hex way?
and when i bin(6) i see '0b101'

I should expect to see 8 bits of 1s and 0's. what the 'b' is tryign to say?

'\x' is an escape sequence and means that the following two characters 
should be interpreted as a number in hexadecimal notation (see also the 
table of allowed escape sequences: 
http://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals 
).


'0b' tells you that the number is printed in binary notation.
Leading zeros are usually discarded when a number is printed:
>>> bin(70)
'0b1000110'
>>> 0b100110 == 0b00100110
True
>>> 0b100110 == 0b00100110
True

It's the same with decimal notation. You wouldn't say 00123 is different 
from 123, would you?


Bye, Andreas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
What if i wanted to sort it out if alphabetically and not by the values?

Thsi worked:

for item in sorted(months.items(),key=lambda num : num[1]):

but this failed:

for item in sorted(months.items()):

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


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 11:16:37 π.μ. UTC+3, ο χρήστης Νικόλαος Κούρας 
έγραψε:
> What if i wanted to sort it out if alphabetically and not by the values?
> 
> 
> 
> Thsi worked:
> 
> 
> 
> for item in sorted(months.items(),key=lambda num : num[1]):
> 
> 
> 
> but this failed:
> 
> 
> 
> for item in sorted(months.items()):
> 
> 
> 
> why?

sorry what i was tryign to say was why not as: for item in 
sorted(months.values()):

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


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
Trying this:

months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 
'Μάϊος':5, 'Ιούνιος':6, \
   'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 
'Νοέμβριος':11, 'Δεκέμβριος':12 }

for key in sorted( months.values() ):
print('''
 %s 
''' % (months[key], key) )


output this:

[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173]   File 
"/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in , 
referer: http://superhost.gr/
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' % 
(months[key], key) ), referer: http://superhost.gr/
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1, referer: 
http://superhost.gr/

KeyError 1 ??!! All i did was to tell python to sort the dictionary values, 
which are just integers.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 11:15:38 π.μ. UTC+3, ο χρήστης Andreas Perstinger 
έγραψε:

What is the difference between len('nikos') and len(b'nikos')
First beeing the length of string nikos in characters while the second being 
the length of an ???


> The python interpreter will represent all values below 256 as ASCII 
> characters if they are printable:

>  >>> ord(b'a')
> 97
>  >>> hex(97)
> '0x61'
>  >>> b'\x61' == b'a'
> True
> The Python designers have decided to use b'a' instead of b'\x61'.

b'a' and b'\x61' are the bytestrings of char 'a' after utf-8 encoding?

This ord(b'a' )should give an error in my opinion:

ord('a') should return the ordinal value of char 'a', not ord(b'a')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 09:34, "Νικόλαος Κούρας"  wrote:
>
> Trying this:
>
> months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4,
'Μάϊος':5, 'Ιούνιος':6, \
>'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10,
'Νοέμβριος':11, 'Δεκέμβριος':12 }
>
> for key in sorted( months.values() ):
> print('''
>  %s 
> ''' % (months[key], key) )
>
>
> output this:
>
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173]   File
"/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in ,
referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' %
(months[key], key) ), referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1,
referer: http://superhost.gr/
>
> KeyError 1 ??!! All i did was to tell python to sort the dictionary
values, which are just integers.
> --
> http://mail.python.org/mailman/listinfo/python-list

KeyError: 1 means that there is no int(1) key. I think you meant to do "for
key in sorted(yourdict.keys())"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
After many tried this did the job:

for key in sorted(months.items(),key=lambda num : num[1]):
print('''
 %s 
''' % (key[1], key[0]) )


but its really frustrating not being able to:

for key in sorted( months.values() ): 
print(''' 
 %s  
''' % (months[key], key) ) 

Which seemed to be an abivous way to do it.
names set() was able to order like this why not the dictionary too?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Νικόλαος Κούρας
>  s = 'α' 
>  s.encode('utf-8') 
> > b'\xce\xb1' 

'b' stands for binary right? 
 b'\xce\xb1' = we are looking at a byte in a hexadecimal format? 
if yes how could we see it in binary and decimal represenation? 
  
> > I see that the encoding of this char takes 2 bytes. But why two exactly? 
> > How do i calculate how many bits are needed to store this char into bytes? 
  
> Because utf-8 takes 1 to 4 bytes to encode characters 

Since 2^8 = 256, utf-8 should store the first 256 chars of unicode charset 
using 1 byte. 

Also Since 2^16 = 65535, utf-8 should store the first 65535 chars of unicode 
charset using 2 bytes and so on. 

But i know that this is not the case. 
But i dont understand why. 


>  s = 'a' 
>  s.encode('utf-8') 
> > b'a' 
> utf-8 takes ASCII as it is, as 1 byte. They are the same 

EBCDIC and ASCII and Unicode are charactet sets, correct? 

iso-8859-1, iso-8859-7, utf-8, utf-16, utf-32 and so on are encoding methods, 
right?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 10:53, "Νικόλαος Κούρας"  wrote:
>
> After many tried this did the job:
>
> for key in sorted(months.items(),key=lambda num : num[1]):
> print('''
>  %s 
> ''' % (key[1], key[0]) )
>
>
> but its really frustrating not being able to:
>
> for key in sorted( months.values() ):
> print('''
>  %s 
> ''' % (months[key], key) )
>
> Which seemed to be an abivous way to do it.
> names set() was able to order like this why not the dictionary too?

Why not

for key, value in sorted(d.items()):

Tuples are totally sortable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Ulrich Eckhardt

Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας:

for key in sorted( months.values() ):

  ^^^   ^^


KeyError 1 ??!! All i did was to tell python to sort the dictionary values, 
which are just integers.


...and which you then proceed to use as key, which is obviously wrong.

Uli

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


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Ulrich Eckhardt

Am 10.06.2013 10:04, schrieb Νικόλαος Κούρας:

months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 
'Μάϊος':5, 'Ιούνιος':6, \
   'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 
'Νοέμβριος':11, 'Δεκέμβριος':12 }

for key in sorted( months.keys() ):


I'm having trouble ordering a dictionary though.


I can't find a problem here. I tried simple dictionaries containing 
numbers as keys using Python 3.3, and sorting the keys works without any 
problem there. What exactly is the "trouble" you are having? Be a bit 
more precise and describe what you saw and, just in case, also what you 
expected to see.


BTW: You have a line continuation there using a backslash. This isn't 
necessary, since the pair of {} automatically tell Python the target range.



Good luck!

Uli


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


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt 
έγραψε:
> Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας:
> 
> > for key in sorted( months.values() ):
> 
>^^^   ^^
> 
> 
> 
> > KeyError 1 ??!! All i did was to tell python to sort the dictionary values, 
> > which are just integers.
> 
> 
> 
> ...and which you then proceed to use as key, which is obviously wrong.

How hsould have i written it then?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Andreas Perstinger

On 10.06.2013 11:59, Νικόλαος Κούρας wrote:

 s = 'α'
 s.encode('utf-8')
> b'\xce\xb1'


'b' stands for binary right?


No, here it stands for bytes:
http://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals


  b'\xce\xb1' = we are looking at a byte in a hexadecimal format?


No, b'\xce\xb1' represents a byte object containing 2 bytes.
Yes, each byte is represented in hexadecimal format.


if yes how could we see it in binary and decimal represenation?


>>> s = b'\xce\xb1'
>>> s[0]
206
>>> bin(s[0])
'0b11001110'
>>> s[1]
177
>>> bin(s[1])
'0b10110001'

A byte object is a sequence of bytes (= integer values) and support 
indexing.

http://docs.python.org/3/library/stdtypes.html#bytes


Since 2^8 = 256, utf-8 should store the first 256 chars of unicode
charset using 1 byte.

Also Since 2^16 = 65535, utf-8 should store the first 65535 chars of
unicode charset using 2 bytes and so on.

But i know that this is not the case. But i dont understand why.


Because your method doesn't work.
If you use all possible 256 bit-combinations to represent a valid 
character, how do you decide where to stop in a sequence of bytes?



 s = 'a'
 s.encode('utf-8')
> b'a'
utf-8 takes ASCII as it is, as 1 byte. They are the same


EBCDIC and ASCII and Unicode are charactet sets, correct?

iso-8859-1, iso-8859-7, utf-8, utf-16, utf-32 and so on are encoding methods, 
right?



Look at http://www.unicode.org/glossary/ for an explanation of all the 
terms.


Bye, Andreas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread Νικόλαος Κούρας
>Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich 
Eckhardt έγραψε:


for key in sorted( months.keys() ):
print('''
 %s 
''' % (months[key], key) )

this in fact works, it sorts the dict by its keys() was mistaken before 
but the sorting aint correct because its done alphabetically and not by 
integer value.

We need values.


for key in sorted( months.values() ):
print('''
 %s 
''' % (key, what should_we put here_to get the actual months 
name? )

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


Encoding questions (continuation)

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 1:42:25 μ.μ. UTC+3, ο χρήστης Andreas 
Perstinger έγραψε:


>  >>> s = b'\xce\xb1'
>
>  >>> s[0]
>
> 206

's' is a byte object, how can you treat it as a string asking to present 
you its first character?


>
>  >>> s[1]
>
> 177

's' is a byte object, how can you treat it as a string asking to present 
you its first character?


> A byte object is a sequence of bytes (= integer values) and support  
indexing


A sequeence of bystes is a a sequence of bits which is zeros and one's 
not integers.



> Because your method doesn't work.
> If you use all possible 256 bit-combinations to represent a valid
> character, how do you decide where to stop in a sequence of bytes?

How you mean? please provice an example so i can understand this.

> > EBCDIC and ASCII and Unicode are charactet sets, correct?

> > iso-8859-1, iso-8859-7, utf-8, utf-16, utf-32 and so on are 
encoding methods, right?


> Look at http://www.unicode.org/glossary/ for an explanation of all 
the terms


I did but docs confuse me even more. Can you pleas ebut it simple.

Unicode as i udnerstand it was created out of need for a big character 
set that could be able to hold all worlds symboles, whiel ascii could 
only store first 127 and extended 246.


ascii and unicode are character sets.

everything else sees to be an encoding system that work upne the 
characters set(never deen them though act on the scii charset)


but iso-8859-7 is both a charset and an encoding system?



ps. i tried to post a reply to the thread i opend via thunderbird mail 
client, but not as a reply to somne other reply but as  new mail send to 
python list.

because of that a new thread will be opened.
How can i tell thunderbird to reply to the original thread and not start 
a new one?
Sorry for that but i can' even post via google groups any more. it says 
that an error occured.
Sorry for this. please xplain hpw to reply properly to correct hread via 
mail.

thank you.
--
Webhost && Weblog 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encoding questions (continuation)

2013-06-10 Thread Steven D'Aprano
On Mon, 10 Jun 2013 14:13:00 +0300, Νικόλαος Κούρας wrote:

> Τη Δευτέρα, 10 Ιουνίου 2013 1:42:25 μ.μ. UTC+3, ο χρήστης Andreas
> Perstinger έγραψε:
> 
>  >  >>> s = b'\xce\xb1'
>  >
>  >  >>> s[0]
>  >
>  > 206
> 
> 's' is a byte object, how can you treat it as a string asking to present
> you its first character?

That is not treating it as a string, and it does not present the first 
character. It presents the first byte, which is a number between 0 and 
255, not a character.

py> alist = [0xce, 0xb1]
py> alist[0]
206

Is that treating alist as a string? No, of course not. Strings are not 
the only object that have indexing object[position].


> 's' is a byte object, how can you treat it as a string asking to present
> you its first character?

You just asked that exact same question. Why ask it twice?


>  > A byte object is a sequence of bytes (= integer values) and support
> indexing
> 
> A sequeence of bystes is a a sequence of bits which is zeros and one's
> not integers.

Nikos, you fail basic computers. Time for you to step away from the 
computer, go to the library, and borrow a book about the basic 
fundamentals of how computers work. Perhaps something written for school 
children.

I am not saying this to insult you, or to be rude. But you are obviously 
struggling with the most basic concepts, like what a byte is. You need to 
go back to basics and learn the simple things, and perhaps if it is 
explained to you in your native language, you will understand it better.


>  > Because your method doesn't work.
>  > If you use all possible 256 bit-combinations to represent a valid
>  > character, how do you decide where to stop in a sequence of bytes?
> 
> How you mean? please provice an example so i can understand this.

I have already provided an example. Many other people have provided 
examples. Please read them.


>  > > EBCDIC and ASCII and Unicode are charactet sets, correct?
> 
>  > > iso-8859-1, iso-8859-7, utf-8, utf-16, utf-32 and so on are
> encoding methods, right?
> 
>  > Look at http://www.unicode.org/glossary/ for an explanation of all
> the terms
> 
> I did but docs confuse me even more. Can you pleas ebut it simple.

Nikos, if you can't be bothered to correct your spelling mistakes, why 
should we be bothered to answer your questions? It takes you half a 
second to fix a typo like "pleas ebut". It takes us five, ten, fifteen, 
twenty minutes to write an email explaining these concepts, and then you 
don't bother to read them and just ask the same question again. And 
again. And again.


> ps. i tried to post a reply to the thread i opend via thunderbird mail
> client, but not as a reply to somne other reply but as  new mail send to
> python list.
> because of that a new thread will be opened. How can i tell thunderbird
> to reply to the original thread and not start a new one?

By replying to an email in that thread.



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


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Steven D'Aprano
On Mon, 10 Jun 2013 03:42:38 -0700, Νικόλαος Κούρας wrote:

> for key in sorted( months.values() ):

> please tell me Uli why this dont work as expected to.

Because values are not keys. You are looking at the values, and trying to 
use them as keys.

months = {'Φεβρουάριος':2, 'Ιανουάριος':1}
print("==Values==")
for x in sorted(months.values()):
print(x)

print("==Keys==")
for x in sorted(months.keys()):
print(x)


prints:


==Values==
1
2
==Keys==
Ιανουάριος
Φεβρουάριος



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


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Steven D'Aprano
On Mon, 10 Jun 2013 00:10:38 -0700, nagia.retsina wrote:

> Τη Κυριακή, 9 Ιουνίου 2013 3:31:44 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
> έγραψε:
> 
>> py> c = 'α'
>> py> ord(c)
>> 945
> 
> The number 945 is the characters 'α' ordinal value in the unicode
> charset correct?

Correct.


> The command in the python interactive session to show me how many bytes
> this character will take upon encoding to utf-8 is:
> 
 s = 'α'
 s.encode('utf-8')
> b'\xce\xb1'
> 
> I see that the encoding of this char takes 2 bytes. But why two exactly?

Because that's how UTF-8 works. If it was a different encoding, it might 
be 4 bytes, or 2, or 1, or 101, or 7, or 3. But it is UTF-8, so it takes 
2 bytes. If you want to understand how UTF-8 works, look it up on 
Wikipedia. 


> How do i calculate how many bits are needed to store this char into
> bytes?

Every byte is made of 8 bits. There are two bytes. So multiply 8 by 2.


> Trying to to the same here but it gave me no bytes back.
> 
 s = 'a'
 s.encode('utf-8')
> b'a'

There is a byte there. The byte is printed by Python as b'a', which in my 
opinion is a design mistake. That makes it look like a string, but it is 
not a string, and would be better printed as b'\x61'. But regardless of 
the display, it is still a single byte.

 
>>py> c.encode('utf-8')
>> b'\xce\xb1'
> 
> 2 bytes here. why 2?

Because that's how UTF-8 works.


>> py> c.encode('utf-16be')
>> b'\x03\xb1'
> 
> 2 byets here also. but why 3 different bytes? 

Because it is a different encoding.


> the ordinal value of char 'a' is the same in unicode.

The same as what?


> the encodign system just takes the ordinal value end encode, but 
> sinc eit uses 2 bytes should these 2 bytes be the same?

No.

That's like saying that since a dog in Germany has four legs and one 
head, and a dog in France has four legs and one head, dog should be 
spelled "Hund" in both Germany and France.

Different encodings are like different languages. They spell the same 
word differently.


>> py> c.encode('utf-32be')
>> b'\x00\x00\x03\xb1
> 
> every char here takes exactly 4 bytes to be stored. okey.
> 
>> py> c.encode('iso-8859-7')
>> b'\xe1'
> 
> And also does '\x' means that the value is being respresented in hex
> way? and when i bin(6) i see '0b101'
> 
> I should expect to see 8 bits of 1s and 0's. what the 'b' is tryign to
> say?

"b" for Binary.

Just like 0o1234 uses octal, "o" for Octal.

And 0x123EF uses hexadecimal. "x" for heXadecimal.



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


Re: Re-using copyrighted code

2013-06-10 Thread Robert Kern

On 2013-06-08 22:31, Malte Forkel wrote:

Hello,

I have written a small utility to locate errors in regular expressions
that I want to upload to PyPI.  Before I do that, I would like to learn
a litte more about the legal aspects of open-source software. What would
be a good introductory reading?


Larry Rosen's free (open source, even!) book _Open Source Licensing_ is good 
introductory reading. Larry is an intellectual property lawyer and helped draft 
the current PSF license.


  http://www.rosenlaw.com/oslbook.htm

--
Robert Kern

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

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


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread Ulrich Eckhardt

Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:

 >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
Eckhardt έγραψε:

for key in sorted( months.keys() ):
 print('''
  %s 
 ''' % (months[key], key) )

this in fact works, it sorts the dict by its keys()


No, it does not sort the dict. Please slow down, relax and take a look 
at the documentation of sorted(). You are jumping to conclusions based 
on flawed expectations and assumptions, which can only yield garbage in 
the end.


Uli

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


Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Ulrich Eckhardt

Am 10.06.2013 11:48, schrieb Νικόλαος Κούρας:

After many tried this did the job:

for key in sorted(months.items(),key=lambda num : num[1]):
print('''
 %s 
''' % (key[1], key[0]) )


This code is still sending a misleading message. What you are referring 
to as "key" here is in fact a (key, value) tuple. I'd use Fábio's 
suggestion and use the automatic splitting:


for name, idx in sorted(months.items(), key=lambda num : num[1]):
print('month #{} is {}'.format(idx, name))



but its really frustrating not being able to:

for key in sorted( months.values() ):
 print('''
  %s 
 ''' % (months[key], key) )

Which seemed to be an abivous way to do it.


You are composing three things:

1. months.values() - gives you a sequence with the month numbers
2. sorted() - gives you a sorted sequence
3. for-iteration - iterates over a sequence

At which point is Python doing anything non-obvious? Also, have you 
considered reversing the dictionary mapping or creating a second one 
with the reversed mapping? Or maybe take a look at collections.OrderedDict?




names set() was able to order like this why not the dictionary too?


Well, why don't you use a set then, if it solves your problem? An in 
which place does anything behave differently? Sorry to bring you the 
news, but your expectations are not fulfilled because your assumptions 
about how things should work are already flawed, I'm afraid.



Uli

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


Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Let:
- class Point be a data type which is used to define points in space
- class Line be a data type which possesses an aggregate relationship with 
objects of type Point
- class Model be a container class which stores collections of Point and 
Line objects


Essentially, a Model object stores lists of Point objects and Line objects, 
and Line objects include references to Point objects which represent the 
starting and ending point of a line.

To reflect this class relationship, I've defined the following classes:


class Point:
position = []

def __init__(self, x, y, z = 0):
self.position = [x, y, z]

class Line:
points = ()
def __init__(self, p_i, p_f):
self.points = (p_i, p_f)

class Model:
points = []
lines = []




It would be nice if, whenever a Point object was updated, the Line objects 
which are associated with it could reflect those updates directly in 
Line.p_i and Line.p_f.

What's the Python way of achieving the same effect?


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


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt 
έγραψε:
> Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:
> 
> >  >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
> 
> > Eckhardt έγραψε:
> 
> >
> 
> > for key in sorted( months.keys() ):
> 
> >  print('''
> 
> >   %s 
> 
> >  ''' % (months[key], key) )
> 
> >
> 
> > this in fact works, it sorts the dict by its keys()
> 
> 
> 
> No, it does not sort the dict. Please slow down, relax and take a look 
> 
> at the documentation of sorted(). You are jumping to conclusions based 
> 
> on flawed expectations and assumptions, which can only yield garbage in 
> 
> the end.

it doe ssort the dict at least for keys() why not for values() too?



for key in sorted( months.keys() ): 
 print(''' 
  %s  
 ''' % (months[key], key) ) 

this in fact works, it sorts the dict by its keys() was mistaken before 
but the sorting aint correct because its done alphabetically and not by 
integer value. 
We need value
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Roy Smith
In article ,
 Rui Maciel  wrote:
 
> Essentially, a Model object stores lists of Point objects and Line objects, 
> and Line objects include references to Point objects which represent the 
> starting and ending point of a line.
>
> class Point:
> position = []
> 
> def __init__(self, x, y, z = 0):
> self.position = [x, y, z]
> 
> class Line:
> points = ()
> def __init__(self, p_i, p_f):
> self.points = (p_i, p_f)
> 
> class Model:
> points = []
> lines = []
> 
>
> It would be nice if, whenever a Point object was updated, the Line objects 
> which are associated with it could reflect those updates directly in 
> Line.p_i and Line.p_f.

Have you tried running the code you wrote?  It does that already!  When 
you do something like:

my_list = [obj1, obj2]

in Python, the objects are stored by reference (not just lists, all 
assignments are by reference).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Peter Otten
Rui Maciel wrote:

> Let:
> - class Point be a data type which is used to define points in space
> - class Line be a data type which possesses an aggregate relationship with
> objects of type Point
> - class Model be a container class which stores collections of Point and
> Line objects
> 
> 
> Essentially, a Model object stores lists of Point objects and Line
> objects, and Line objects include references to Point objects which
> represent the starting and ending point of a line.
> 
> To reflect this class relationship, I've defined the following classes:
> 
> 
> class Point:

Don't add 

> position = []

to your code. That's not a declaration, but a class attribute and in the 
long run it will cause nothing but trouble.

> def __init__(self, x, y, z = 0):
> self.position = [x, y, z]
> 
> class Line:
> points = ()
> def __init__(self, p_i, p_f):
> self.points = (p_i, p_f)
> 
> class Model:
> points = []
> lines = []
> 
> 
> 
> 
> It would be nice if, whenever a Point object was updated, the Line objects
> which are associated with it could reflect those updates directly in
> Line.p_i and Line.p_f.
> 
> What's the Python way of achieving the same effect?

Your code already does that. Here's a minimally cleaned up version:

$ cat tmp_points.py
class Point:
def __init__(self, x, y, z=0):
self.position = (x, y, z)
def __str__(self):
return "%s" % (self.position,)

class Line:
def __init__(self, p_i, p_f):
self.points = (p_i, p_f)
def __str__(self):
return "%s-->%s" % self.points

a = Point(1, 2, 3)
b = Point(4, 5, 6)
ab = Line(a, b)
print(ab)
a.position = (10, 20, 30)
print(ab)

$ python3 tmp_points.py 
(1, 2, 3)-->(4, 5, 6)
(10, 20, 30)-->(4, 5, 6)


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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Roy Smith wrote:

> Have you tried running the code you wrote?  It does that already!  When
> you do something like:
> 
> my_list = [obj1, obj2]
> 
> in Python, the objects are stored by reference (not just lists, all
> assignments are by reference).

I've tested the following:


model = Model()
model.points.append(Point(1,2))
model.points.append(Point(1,4))

line = Line( model.points[0], model.points[1])

# Case A: this works
model.points[0].position = [2,3,4]
line.points

# Case B: this doesn't work
test.model.points[0] = test.Point(5,4,7)
line.points



Is there a Python way of getting the same effect with Case B?


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


Re: Encoding questions (continuation)

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 2:41:07 μ.μ. UTC+3, ο χρήστης Steven D'Aprano 
έγραψε:
> On Mon, 10 Jun 2013 14:13:00 +0300, Νικόλαος Κούρας wrote:
> 
> 
> 
> > Τη Δευτέρα, 10 Ιουνίου 2013 1:42:25 μ.μ. UTC+3, ο χρήστης Andreas
> 
> > Perstinger έγραψε:
> 
> > 
> 
> >  >  >>> s = b'\xce\xb1'
> 
> >  >
> 
> >  >  >>> s[0]
> 
> >  >
> 
> >  > 206
> 
> > 
> 
> > 's' is a byte object, how can you treat it as a string asking to present
> 
> > you its first character?
> 
> 
> 
> That is not treating it as a string, and it does not present the first 
> 
> character. It presents the first byte, which is a number between 0 and 
> 
> 255, not a character.
> 
> 
> 
> py> alist = [0xce, 0xb1]
> 
> py> alist[0]
> 
> 206


To my mind alist[0] should yield '0xce'

> Is that treating alist as a string? No, of course not. Strings are not 
> 
> the only object that have indexing object[position].

Yes actually it does.

s string is a series of characters.

a list is a series of objects, which can be chars, strings, integers, other 
data structures.

So doing a_list[0] is similar of doing a_string[00


> >  > A byte object is a sequence of bytes (= integer values) and support 
> > indexing


Isn't a byte a series of zeros and ones, like 01010101 ?
So why you say bytes are integers since its numbers into a binary system?
perhsp you mean a represantaion of a bye to a decimal value?

> I am not saying this to insult you, or to be rude. But you are obviously  
> struggling with the most basic concepts, like what a byte is. You need to  
> go back to basics and learn the simple things, and perhaps if it is 
> explained to you in your native language, you will understand it better.
> 

> I have already provided an example. Many other people have provided 
> 
> examples. Please read them.

i do read everythign being posted back to me.

> > ps. i tried to post a reply to the thread i opend via thunderbird mail
> > client, but not as a reply to somne other reply but as  new mail send to
> > python list.
> > because of that a new thread will be opened. How can i tell thunderbird
> > to reply to the original thread and not start a new one?
> By replying to an email in that thread.

Yes thats obvious.
What is not obvious is how you reply back to a thread by giving extra info when 
you are not replying to a mail formt tha thread or when you ahve deleted the 
reply for a member

sending the mail to python-list@python.org will just open anew subject intead 
of replyign to an opened thread.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Peter Otten wrote:

> Don't add
> 
>>position = []
> 
> to your code. That's not a declaration, but a class attribute and in the
> long run it will cause nothing but trouble.

Why's that?


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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Rui Maciel wrote:

> # Case B: this doesn't work
> test.model.points[0] = test.Point(5,4,7)

Disregard the "test." bit.  I was testing the code by importing the 
definitions as a module.


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


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt 
έγραψε:
> Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:
> 
> >  >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
> 
> > Eckhardt έγραψε:
> 
> >
> 
> > for key in sorted( months.keys() ):
> 
> >  print('''
> 
> >   %s 
> 
> >  ''' % (months[key], key) )
> 
> >
> 
> > this in fact works, it sorts the dict by its keys()
> 
> 
> 
> No, it does not sort the dict. Please slow down, relax and take a look 
> 
> at the documentation of sorted(). You are jumping to conclusions based 
> 
> on flawed expectations and assumptions, which can only yield garbage in 
> 
> the end.
> 
> 
> 
> Uli

It soerts it just fine by keys() alpabeticall but this isnt what i want, i need 
sorting by values()

for key in sorted( months.values() ): 
 print(''' 
  %s  
 ''' % (key, what should_we put here_to get the actual months 
name? ) 

the value is a vale form  the values in order, but how do i specify the 
correspondign key to that value?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re-using copyrighted code

2013-06-10 Thread Ethan Furman

On 06/10/2013 05:57 AM, Robert Kern wrote:

On 2013-06-08 22:31, Malte Forkel wrote:

Hello,

I have written a small utility to locate errors in regular expressions
that I want to upload to PyPI.  Before I do that, I would like to learn
a litte more about the legal aspects of open-source software. What would
be a good introductory reading?


Larry Rosen's free (open source, even!) book _Open Source Licensing_ is good 
introductory reading. Larry is an
intellectual property lawyer and helped draft the current PSF license.

   http://www.rosenlaw.com/oslbook.htm


Nice, thanks for the link!

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Peter Otten
Rui Maciel wrote:

> Peter Otten wrote:
> 
>> Don't add
>> 
>>>position = []
>> 
>> to your code. That's not a declaration, but a class attribute and in the
>> long run it will cause nothing but trouble.
> 
> Why's that?

Especially with mutable attributes it's hard to keep track whether you are 
operating on the instance or the class:

>>> class Point:
... position = []
... def __init__(self, x, y, z):
... self.position = [x, y, z]
... 
>>> a = Point(1, 2, 3)
>>> b = Point(10, 20, 30)
>>> a.position
[1, 2, 3]
>>> del a.position
>>> a.position
[] # did you expect that?
>>> del b.position
>>> b.position.extend(["did you expect that?"])
>>> a.position
['did you expect that?']
>>> del a.position
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: position


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


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 2:59:03 μ.μ. UTC+3, ο χρήστης Steven D'Aprano 
έγραψε:
> On Mon, 10 Jun 2013 00:10:38 -0700, nagia.retsina wrote:
> 
> 
> 
> > Τη Κυριακή, 9 Ιουνίου 2013 3:31:44 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
> 
> > έγραψε:
> 
> > 
> 
> >> py> c = 'α'
> 
> >> py> ord(c)
> 
> >> 945
> 
> > 
> 
> > The number 945 is the characters 'α' ordinal value in the unicode
> 
> > charset correct?
> 
> 
> 
> Correct.
> 
> 
> 
> 
> 
> > The command in the python interactive session to show me how many bytes
> 
> > this character will take upon encoding to utf-8 is:
> 
> > 
> 
>  s = 'α'
> 
>  s.encode('utf-8')
> 
> > b'\xce\xb1'
> 
> > 
> 
> > I see that the encoding of this char takes 2 bytes. But why two exactly?
> 
> 
> 
> Because that's how UTF-8 works. If it was a different encoding, it might 
> 
> be 4 bytes, or 2, or 1, or 101, or 7, or 3. But it is UTF-8, so it takes 
> 
> 2 bytes. If you want to understand how UTF-8 works, look it up on 
> 
> Wikipedia. 
> 
> 
> 
> 
> 
> > How do i calculate how many bits are needed to store this char into
> 
> > bytes?
> 
> 
> 
> Every byte is made of 8 bits. There are two bytes. So multiply 8 by 2.
> 
> 
> 
> 
> 
> > Trying to to the same here but it gave me no bytes back.
> 
> > 
> 
>  s = 'a'
> 
>  s.encode('utf-8')
> 
> > b'a'
> 
> 
> 
> There is a byte there. The byte is printed by Python as b'a', which in my  
> opinion is a design mistake. That makes it look like a string, but it is  
> not a string, and would be better printed as b'\x61'. But regardless of 
> the display, it is still a single byte.


Perhaps, up to 127 ASCII chars python thinks its better for human to read the 
character representaion of the stored byte, instead of hex's. Just a guess.

> Just like 0o1234 uses octal, "o" for Octal.
> And 0x123EF uses hexadecimal. "x" for heXadecimal.

Why the leadin zero before octal's 'o' and hex's 'x'  and binary's 'b' ?


Iam not goin to tired you any more, because ia hve exhaust myself tlo days now 
tryign to get my head around this.

Please confirm i ahve understood correctly:

I did but docs confuse me even more. Can you pleas ebut it simple.

Unicode as i understand it was created out of need for a bigger character set 
compared to ASCII which could hold up to 127 chars(and extended versions of it 
up to 256), that could be able to hold all worlds symbols.

ASCII and Unicode are character sets.

Everything else sees to be an encoding system that work upon those characters 
sets.

If what i said is true the last thing that still confuses me is that

iso-8859-7(256 chars) seems like charactet set and an encoding method too.
Can it be both or it is iso-8859-7 encoding method of Unicode character set 
similar as uTF8 is also Unicode's encoding method?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread Νικόλαος Κούρας
Since dict.keys() return a list of the keys in the dict and the keys are 
associated with the dict's values why doesnt it work the other way around too?

I'm talking about this:
[code]
for key in sorted( months.keys() ): 
 print(''' 
  %s  
 ''' % (months[key], key) ) 

[/code]

I could have uses ordered by kees but that orders key alphabetically, and 
montha re not displayed in the usual row. ordering by values though it would be 
a perfect fit.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Peter Otten wrote:

> Rui Maciel wrote:
> 
>> Peter Otten wrote:
>> 
>>> Don't add
>>> 
position = []
>>> 
>>> to your code. That's not a declaration, but a class attribute and in the
>>> long run it will cause nothing but trouble.
>> 
>> Why's that?
> 
> Especially with mutable attributes it's hard to keep track whether you are
> operating on the instance or the class:
> 
 class Point:
> ... position = []
> ... def __init__(self, x, y, z):
> ... self.position = [x, y, z]
> ...
 a = Point(1, 2, 3)
 b = Point(10, 20, 30)
 a.position
> [1, 2, 3]
 del a.position
 a.position
> [] # did you expect that?
 del b.position
 b.position.extend(["did you expect that?"])
 a.position
> ['did you expect that?']
 del a.position
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: position


How do you guarantee that any object of a class has a specific set of 
attributes?


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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Peter Otten
Rui Maciel wrote:

> How do you guarantee that any object of a class has a specific set of
> attributes?

You don't. 

Such a guarantee is like the third wheel on a bike -- it doesn't improve the 
overall experience.

PS: I'd rather not mention the memory-saving technique that is sometimes 
abused, but here goes: 

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


testfixtures 3.0.1 Released!

2013-06-10 Thread Chris Withers

Hi All,

I'm pleased to announce the release of testfixtures 3.0.1. This is a bug 
fix release featuring the following changes:


- Some documentation tweaks and clarifications.

- Fixed a bug which masked exceptions when using compare() with a broken 
generator.


- Fixed a bug when comparing a generator with a non-generator.

- Ensure LogCapture cleans up global state it may effect.

- Fixed replacement of static methods using a Replacer.

If you haven't bumped into it already, testfixtures is a collection of 
helpers for writing succinct unit tests including help for:


- Comparing objects and sequences

Better feedback when the results aren't as you expected along with 
support for comparison of objects that don't normally support comparison.


- Mocking out objects and methods

Easy to use ways of stubbing out objects, classes or individual methods 
for both doc tests and unit tests. Special helpers are provided for 
testing with dates and times.


- Testing logging

Helpers for capturing logging output in both doc tests and unit tests.

- Testing stream output

Helpers for capturing stream output, such as that from print statements, 
and making assertion about it.


- Testing with files and directories

Support for creating and checking files and directories in sandboxes for 
both doc tests and unit tests.


- Testing exceptions

Easy to use ways of checking that a certain exception is raised, even 
down the to the parameters the exception is raised with.


The package is on PyPI and a full list of all the links to docs, issue 
trackers and the like can be found here:


http://www.simplistix.co.uk/software/python/testfixtures

Any questions, please do ask on the Testing in Python list or on the 
Simplistix open source mailing list...


cheers,

Chris

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


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread Ulrich Eckhardt

Am 10.06.2013 15:37, schrieb Νικόλαος Κούρας:

Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt 
έγραψε:

Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:


Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich

Eckhardt έγραψε:







for key in sorted( months.keys() ):



  print('''



   %s 



  ''' % (months[key], key) )







this in fact works, it sorts the dict by its keys()




No, it does not sort the dict. Please slow down, relax and take a look
at the documentation of sorted(). You are jumping to conclusions based
on flawed expectations and assumptions, which can only yield garbage in
the end.


it doe ssort the dict at least for keys() why not for values() too?


Well, because it does not sort the dict, it sorts the sequence that you 
pass into sorted(). The dictionary that you retrieved from is not 
modified. Which part of the documentation is unclear to you? Did you 
even bother reading the docs?




for key in sorted( months.keys() ):
  print('''
   %s 
  ''' % (months[key], key) )

this in fact works, it sorts the dict by its keys() was mistaken before
but the sorting aint correct because its done alphabetically and not by
integer value.


Listen: Computers will always do what you tell them to. If you tell them 
garbage, they will do garbage. If that is not what you want them to do, 
it's your own fault. That means that you have to precisely(!!!) describe 
what you want when talking to a computer. The computer will not try to 
guess what you might have wanted.


Now, the above claim, that "it sorts the dict by its keys()" is simply 
wrong. Instead, it outputs the dictionary's elements sorted by their 
key. There is a fine distinction between the two. I know what you mean, 
because I'm a human being and I can copy with your vague description, 
but the computer doesn't.


Good luck, I'm outta here

Uli

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Peter Otten wrote:

> Rui Maciel wrote:
> 
>> How do you guarantee that any object of a class has a specific set of
>> attributes?
> 
> You don't.


What's your point regarding attribute assignments in class declarations, 
then?


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


py_compile vs. built-in compile, with __future__

2013-06-10 Thread dhyams
The built-in compile() function has a "flags" parameter that one can use to 
influence the "__future__" mechanism. However, py_compile.compile, which I'm 
using to byte-compile code, doesn't have an equivalent means to do this.

Is this by design, or would this be considered a bug?  I'm just wanting to do 
something like:

import __future__
py_compile.compile("foobar.py",flags=__future__.CO_FUTURE_DIVISION)

but there is no "flags" parameter to py_compile.compile().

It seems like this would be fixed just as easily as adding a "flags" parameter 
to py_compile.compile(), and then passing that through to the built-in 
compile(), which is called inside of py_compile.compile().
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reply to post 'Tryign to add a valkue to a set'

2013-06-10 Thread russ . pobox
for key, value in sorted(months.items(), key=lambda x:x[1]):
print("""'\t%s'\n""" % (value, key))


Explanation:
- - - - - - 
dict.items is a method associated with dicts just like dict.keys or 
dict.values, and returns a list of (key, value) pairs.

sorted and some other builtin functions have an optional key argument, which 
tells the function what exactly to look at when sorting the sequence. (I say 
sequence because you can sort a string or tuple or dict and get a list back in 
return). In this case we use a simple lambda function to tell it to look at the 
value not the key (i.e. (key, value)[1] returns value)

Alternatively you could do this.

def get_value(item):
return item[1]

for key, value in sorted(months.items(), key=get_value):
...etc...

You might also wonder what this would do

for key, value in sorted(months.items()):
...etc...

Which becomes a question of what would something like this do

sorted([(1,3), (2,2), (3,1)])

Well sorted is a function that expects to get some iterable (sequence of some 
kinda items) to sort. If those items happens to be sequences themselves, like 
(key, value) which is a sequence of two items, then it's only going to care 
about the first item and ignore the rest (i.e ignore value).

So the above will return
[(1,3), (2,2), (3,1)] which as far as sorted() is concerned, is already sorted.


About the string being printed:
- - - - - - - - - - - - - - - - 
I'm not sure why or if you want a single quotation around the entire element to 
be printed as well but if so then that's how I'd do it. The \t is for a tab. 
You also don't have to worry about adding newlines because print will print 
each statement on a newline anyways by default unless you tell it otherwise by 
specifying the sep argument.

Sorry my first post, I said allot didn't I.
-- 
http://mail.python.org/mailman/listinfo/python-list


Questions on "import" and "datetime"

2013-06-10 Thread Yunfei Dai
Hi all,

I have some questions on "import":

1."from datetime import datetime" works well. But I am confused why "import 
datetime.datetime" leads to importerror. "from xlrd import open_workbook" could 
be replaced by "from xlrd.open_workbook" without any problem. The only 
difference here is that if "from xlrd import open_workbook" is used we do not 
have to write "xlrd.open_workbook" in the following code but just 
"open_workbook". So my understanding of the difference is "from...import..." 
shortens the code (just like "using namespace std" in C++) but maybe leads to 
name clash. But what is the problem of datetime? 

2.I am also comfused that "datetime.datetime" is a function but whithin 
"datetime.datetime" there are lots of other functions. So what is the type of 
"datetime.datetime" on earth? is it a function, or a class or a folder(library) 
here?

Thanks very much in advance! Very looking forward to your answers.

Best,
Yunfei




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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Peter Otten
Rui Maciel wrote:

> Peter Otten wrote:
> 
>> Rui Maciel wrote:
>> 
>>> How do you guarantee that any object of a class has a specific set of
>>> attributes?
>> 
>> You don't.
> 
> 
> What's your point regarding attribute assignments in class declarations,
> then?

I don't understand the question. My original point was that you should omit 
class attributes that don't fulfill a technical purpose.

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Peter Otten wrote:

> I don't understand the question. My original point was that you should
> omit class attributes that don't fulfill a technical purpose.
> 

You've said the following:


> class Point:

Don't add 

> position = []

to your code. That's not a declaration, but a class attribute and in the 
long run it will cause nothing but trouble.



We've established that you don't like attribute declarations, at least those 
you describe as not fulfill a technical purpose.  What I don't understand is 
why you claim that that would "cause nothing but trouble".


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


Re: Re-using copyrighted code

2013-06-10 Thread llanitedave
On Sunday, June 9, 2013 2:08:54 PM UTC-7, zipher wrote:
> 
> 
> > Fair use has nothing to do with money. It depends on how the work is
> 
> > used and how you've changed it. Weird Al's song parodies are fair use,
> 
> > even though he sells them.
> 
> 
> 
> That can't really be claimed without a case being brought against him.
> 
>  Michael Jackson, for example, probably could have made a case against
> 
> WierdAl, but did not -- that does not automatically mean that
> 
> WierdAl's use was fair-use in the slightest.  In fact, it probably was
> 
> not, but MJ made enough money that he probably also didn't want to the
> 
> PR loss.
> 
> 
> 

Weird Al can be a complex case, because sometimes his songs are true parodies, 
and sometimes they're more satires.  Parody has a pretty firm history of being 
protected under fair use, and Weird Al's MJ-inspired songs ("Fat" and "Eat It") 
are clearly parodies.  (As is his more recent Lady Gaga sendup "Perform This 
Way", while his Star wars saga "The Story Begins" and Coolio-esque "Amish 
Paradise" are more like satires).

So in the case of Weird Al's Michael Jackson parodies, he would be protected 
under law if MJ had decided to sue.

However, there's another reason that Weird Al's "victims" never file a suit.  
First, he always gets permission from them BEFORE publishing a song.  Second, 
the objects of his skewering usually like the fact that they've been noticed by 
him.  Madonna actually suggested the idea of "Like a Surgeon", and when he did 
"Smells Like Nirvana", the group felt like they'd finally made it.

This is all kind of OT, of course, except to point out that fair use is not as 
straightforward as it might seem, but neither is prohibition of reuse.

However, I have yet to see an example of source code that qualifies as either 
parody or satire under any standard.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encoding questions (continuation)

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 15:04, "Νικόλαος Κούρας"  wrote:
>
> Τη Δευτέρα, 10 Ιουνίου 2013 2:41:07 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
έγραψε:
> > On Mon, 10 Jun 2013 14:13:00 +0300, Νικόλαος Κούρας wrote:
> >
> >
> >
> > > Τη Δευτέρα, 10 Ιουνίου 2013 1:42:25 μ.μ. UTC+3, ο χρήστης Andreas
> >
> > > Perstinger έγραψε:
> >
> > >
> >
> > >  >  >>> s = b'\xce\xb1'
> >
> > >  >
> >
> > >  >  >>> s[0]
> >
> > >  >
> >
> > >  > 206
> >
> > >
> >
> > > 's' is a byte object, how can you treat it as a string asking to
present
> >
> > > you its first character?
> >
> >
> >
> > That is not treating it as a string, and it does not present the first
> >
> > character. It presents the first byte, which is a number between 0 and
> >
> > 255, not a character.
> >
> >
> >
> > py> alist = [0xce, 0xb1]
> >
> > py> alist[0]
> >
> > 206
>
>
> To my mind alist[0] should yield '0xce'

This happens because 0xce is an integer object too. Integer objects have no
"memory" of what format they were declared on. Oxce just happens to be 206
in hexadecimal. Ob10 would also result in the integer 2.

> > Is that treating alist as a string? No, of course not. Strings are not
> >
> > the only object that have indexing object[position].
>
> Yes actually it does.
>
> s string is a series of characters.
>
> a list is a series of objects, which can be chars, strings, integers,
other data structures.
>
> So doing a_list[0] is similar of doing a_string[00
>
>
> > >  > A byte object is a sequence of bytes (= integer values) and support
> > > indexing
>
>
> Isn't a byte a series of zeros and ones, like 01010101 ?
> So why you say bytes are integers since its numbers into a binary system?
> perhsp you mean a represantaion of a bye to a decimal value?

A byte is a number, stored in (usually) 8 binary digits. By addressing a
bytes object by its index, you get an integer of that byte. That's just how
python deals with it.

You should heed Steven's advice and read up on basic computing. You seem to
be lacking a lot of basic concepts.

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


PDF in a Bottle - creating PDF using xtopdf, ReportLab, Bottle and Python

2013-06-10 Thread vasudevram

Hi list,

Might be of interest:

PDF in a Bottle - creating PDF using xtopdf, ReportLab, Bottle and Python

http://jugad2.blogspot.in/2013/05/pdf-in-bottle-creating-pdf-using-xtopdf.html

- Vasudev Ram
Python, Linux and open source training and development
www.dancingbison.com


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


Re: Questions on "import" and "datetime"

2013-06-10 Thread Zachary Ware
On Mon, Jun 10, 2013 at 10:37 AM, Yunfei Dai  wrote:
> Hi all,

Hi Yunfei,

>
> I have some questions on "import":
>
> 1."from datetime import datetime" works well. But I am confused why "import 
> datetime.datetime" leads to importerror. "from xlrd import open_workbook" 
> could be replaced by "from xlrd.open_workbook" without any problem.

I assume you mean "import xlrd.open_workbook" here, as "from
xlrd.open_workbook" would be a SyntaxError :)

> The only difference here is that if "from xlrd import open_workbook" is used 
> we do not have to write "xlrd.open_workbook" in the following code but just 
> "open_workbook". So my understanding of the difference is "from...import..." 
> shortens the code (just like "using namespace std" in C++) but maybe leads to 
> name clash.

"from ... import ..." imports an object from a module and assigns it
to a local name that is the same as the name in the other module.  In
other words, the following two examples do the same thing:

from foo import bar

import foo;bar = foo.bar

If foo.bar happens to be a module (module 'bar' in package 'foo'), you
could also do this:

import foo.bar as bar

...and that restriction is where your problem lies.

> But what is the problem of datetime?

I'm not familiar with xlrd, but I believe the difference between
xlrd.open_workbook and datetime.datetime would be that
xlrd.open_workbook is a module in a package, while datetime.datetime
is a class in a module.  'from ... import ...' can import any object
from the target module/package, and assign it to a local name.
'import ...' on the other hand can only import a module (you'll notice
the ImportError you get when you try 'import datetime.datetime' is 'No
module named datetime'.  This particular example is a bit confusing
due to there being a class in a module of the same name, but try
'import datetime.date' for a clearer message.

> 2.I am also comfused that "datetime.datetime" is a function but whithin 
> "datetime.datetime" there are lots of other functions. So what is the type of 
> "datetime.datetime" on earth? is it a function, or a class or a 
> folder(library) here?

datetime.datetime is actually a type of type 'type' (as can be seen
with 'import datetime;type(datetime.datetime)').  In Python 2, this
means it is a new-style class (meaning it is a subclass of 'object').
In Python 3, it's just a class (since there are no longer old-style
classes).

> Thanks very much in advance! Very looking forward to your answers.
>
> Best,
> Yunfei
>

I hope I have actually answered your question and not just muddied
things further for you.  You can of course ask again if I've made
things worse :)

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


Re: Simple converter of files into their hex components... but i can't arrange utf-8 parts!

2013-06-10 Thread blatt447477
Hi Chris,
your critics are welcome! But perhaps the majority of them has been
caused by font problems in my posting.
Google should put as default a "mono" font!
Or perhaps it has been a mistake on my part to not configure
correctly the output of my post (I even didn't change my nickname...
so you read a selfish "me" instead of "Blatt"!).
I will try in this reply to have a more readable output, but I'm not
sure... (I need help from experts!...).
By now, the only advice is to copy/paste my post in an editor.

Coming to your critics...

01
>> Functions and classes are entirely optional in Python :)
Together with my not being 'pythonic', is correct...
but it is so difficult to change programming style at my age!...

02
> 004 # qwerty: not _ unicode but _ ascii
> ___ 2 7767773 667 _ 766 677 _ 676660
> ___ 3 175249a ef4 _ 5e93f45 254 _ 13399a
>
> 005 # qwerty: non è unicode bensì ascii
> ___ 2 7767773 666 ca 766 6667ca 676660
> ___ 3 175249a efe 38 5e93f45 25e33c 13399a

>>I'm not 100% sure of what you're trying to accomplish here.

As you can see from the corrected output (I hope!), I indeed
am trying to produce a hex-dump output ('_' is a "place-holder"
for space which itself is not considered for hex output).
The 2s and 3s are not lines 2 and 3, but 23 in vertical (hex of #)!
In the editor  you can better see my problem with utf-8 chars...
They cause the lack of synchronization. For example 'unicode' after
the utf-8 char is no more synchronized with it's corresponding hex.
To keep the synchronization, you should insert a space after the
utf-8 char... but I didn't succeeed (at least programmatically...).

03
>> ...you consider targeting Python 3.
I probably will never use Python 3! I am perfectly happy with bugfixes
and security patches...
The reason is that I don't need all the "bells and whistles" of Py3,
especially in the field of OOP!

04
>> You can do (synchronization) by simply opening the file as
>> Latin-1 (iso-8859-1) instead of UTF-8.
I already synchronized the output... but afterwards in the editor...
I want to do this programmatically, so I can use it from the
consolle with bash and pipes.

05
>> Or perhaps it'd be better to string them out further vertically?
I considered this solution, but if I succeed in synchronizig all
programmatically... there is not such a difference.

06
>> ... reading the entire file in before producing any output
No problems of speed...

07
>> lP=[]
>> I have no idea what this name is supposed to represent
Simply a list initialization to empty...

08
>> Here's a really fancy trick you can do: Slicing with a step.
Your solution is much better!

I can go further, but it's better that you run my script (if you want)
to get a better understanding. If you are really interested, you can also
try your version (probably better... I will try it).

Bye, Blatt.


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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Peter Otten
Rui Maciel wrote:

> Peter Otten wrote:
> 
>> I don't understand the question. My original point was that you should
>> omit class attributes that don't fulfill a technical purpose.
>> 
> 
> You've said the following:
> 
> 
>> class Point:
> 
> Don't add
> 
>> position = []
> 
> to your code. That's not a declaration, but a class attribute and in the
> long run it will cause nothing but trouble.
> 
> 
> 
> We've established that you don't like attribute declarations, at least
> those
> you describe as not fulfill a technical purpose.  What I don't understand
> is why you claim that that would "cause nothing but trouble".

Have you read the code in the interpreter session I posted? 

If you do not agree that the demonstrated behaviour is puzzling I'll have to 
drop my claim...
Likewise if you can show a benefit of the

>> position = []

line.

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Peter Otten wrote:

> Have you read the code in the interpreter session I posted?
> 
> If you do not agree that the demonstrated behaviour is puzzling I'll have
> to drop my claim...

I don't see how it should be puzzling.  You've deleted the attribute, so it 
ceassed to exist.


> Likewise if you can show a benefit of the
> 
>>>position = []
> 
> line.

I wrote the code that way to declare intent and help document the code.  In 
this case that the class Point is expected to have an attribute named 
position which will point to a list.


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


Build Python 2.7.5 - Modules missing

2013-06-10 Thread Walter Hurry
On building Python 2.7.5 I got the following message:

Python build finished, but the necessary bits to build these modules
 were not found:
dl imageoplinuxaudiodev   
spwd   sunaudiodev
To find the necessary bits, look in setup.py in detect_modules()
 for the module's name.

It carried on with the installation OK, but I don't understand the last 
sentence in the message. How can I find out exactly what modules are 
missing, and what I need to do to make sure they are built next time?

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


Re: Build Python 2.7.5 - Modules missing

2013-06-10 Thread Skip Montanaro
> It carried on with the installation OK, but I don't understand the last
> sentence in the message. How can I find out exactly what modules are
> missing, and what I need to do to make sure they are built next time?

Some of them won't ever build, as they are platform-dependent.  For
example, if you're building on a Linux machine, sunaudiodev won't
build.

The last sentence is just telling you to poke around in the setup.py
code to figure out what it's looking for.  Many libraries have arcane
version naming schemes, and it might not stumble upon the right
spelling, and thus fail to find it.  For example tcl 8.5 might be
installed as libtcl85.so, libtcl8.5.dylib, etc.  I believe the modules
which failed for you are probably all named in a straightforward
fashion, so missing underlying libraries are probably the culprit.

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


Popen and reading stdout in windows

2013-06-10 Thread Joseph L. Casale
I have a use where writing an interim file is not convenient and I was hoping to
iterate through maybe 100k lines of output by a process as its generated or
roughly anyways.

Seems to be a common question on ST, and more easily solved in Linux.
Anyone currently doing this with Python 2.7 in windows and can share some
guidance?

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


Re: Questions on "import" and "datetime"

2013-06-10 Thread Dave Angel

On 06/10/2013 01:01 PM, Zachary Ware wrote:

On Mon, Jun 10, 2013 at 10:37 AM, Yunfei Dai  wrote:

Hi all,


Hi Yunfei,



I have some questions on "import":

1."from datetime import datetime" works well. But I am confused why "import datetime.datetime" 
leads to importerror. "from xlrd import open_workbook" could be replaced by "from 
xlrd.open_workbook" without any problem.




It's a historical flaw in datetime that the class has the same name as 
the module it's in.  It should have been called class Datetime 
(according to pep 10, class names should be capitalized).  If that were 
the case, it'd be clear that datetime.Datetime is a class.


It would also be clearer that
from datetime import Datetime

creates an alias in the present global namespace for the 
datetime.Datetime class, as simply Datetime.


This class has attributes that you can access, like Datetime.hour, and 
it has static methods like Datetime.fromTimeStamp().
 And like all classes, it's "callable", meaning that you can create an 
instance by pretending it's a function:

   obj = Datetime(2013, 12, 1)

But since it's not a module inside a package, you can't use the form:

import datetime.Datetime

Now, just interpret all the above with a lowercase "D" and the confusion 
becomes clearer.  The compiler/interpreter doesn't care either way.


   

"from ... import ..." imports an object from a module and assigns it
to a local name that is the same as the name in the other module.  In
other words, the following two examples do the same thing:

from foo import bar

import foo;bar = foo.bar

If foo.bar happens to be a module (module 'bar' in package 'foo'), you
could also do this:

import foo.bar as bar

...and that restriction is where your problem lies.



In other words, since datetime.datetime is a class, not a module, you 
can't just import it.






2.I am also comfused that "datetime.datetime" is a function but whithin 
"datetime.datetime" there are lots of other functions. So what is the type of 
"datetime.datetime" on earth? is it a function, or a class or a folder(library) here?




As I said before, datetime.datetime is a class, and the functions within 
it are called methods.


You can see it all for yourself very easily.  Use the __file__ attribute 
to locate the source for datetime module on your system.  Here's what it 
looks like on mine:


>>> import datetime
>>> datetime.__file__
'/usr/local/lib/python3.3/datetime.py'

Then you can go look at that file.  For my copy, the datetime class 
begins at 1301.  But you can just search for the following line:




class datetime(date):

HTH

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


Re: Popen and reading stdout in windows

2013-06-10 Thread Grant Edwards
On 2013-06-10, Joseph L. Casale  wrote:

> I have a use where writing an interim file is not convenient and I
> was hoping to iterate through maybe 100k lines of output by a process
> as its generated or roughly anyways.
>
> Seems to be a common question on ST, and more easily solved in Linux.
> Anyone currently doing this with Python 2.7 in windows and can share
> some guidance?

http://docs.python.org/2/library/subprocess.html

-- 
Grant Edwards   grant.b.edwardsYow! All of life is a blur
  at   of Republicans and meat!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re-using copyrighted code

2013-06-10 Thread Joshua Landau
On 10 June 2013 17:29, llanitedave  wrote:
> However, I have yet to see an example of source code that qualifies as either 
> parody or satire under any standard.

You should try reading Perl.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Dave Angel

On 06/10/2013 01:42 PM, Rui Maciel wrote:

Peter Otten wrote:


Have you read the code in the interpreter session I posted?

If you do not agree that the demonstrated behaviour is puzzling I'll have
to drop my claim...


I don't see how it should be puzzling.  You've deleted the attribute, so it
ceassed to exist.


Clearly you didn't reason through all the code in Peter's example.





Likewise if you can show a benefit of the


position = []


line.


I wrote the code that way to declare intent and help document the code.  In
this case that the class Point is expected to have an attribute named
position which will point to a list.



So why do you also have an instance attribute of the same name?  If you 
really want all instances to have the same value for position, you'd 
better change the __init__() function so it doesn't mask that single value.


Could it be that you really meant that instances of class Point are each 
expected to have an attribute named Position?  Could it be that you want 
each instance's position to be independent of the others?


By having a class attribute with the same name as the instance 
attribute, you're occasionally going to use the class version when you 
meant the instance one.


I suspect you didn't realize the distinction between class attributes 
and instance attributes, and that a comment would be a much better way 
to communicate than creating a misleading value.


In one of your other messages, you asked:

> How do you guarantee that any object of a class has a
>  specific set of attributes?

Answer is to define those INSTANCE attributes in the __init__() method 
(or occasionally in the __new__() method), and to make sure you don't 
ever delete them.  The attributes of the object are instance attributes, 
while the attributes defined inside the class are class attributes.


Occasionally, it can be useful to let a class attribute be a 'backup' to 
the instance attributes, but you've got to think through your use case. 
 If memory is really tight, and if nearly all of the instances want the 
same value, then you could omit the instance attribute except for the 
exceptional ones, and let the class attribute fill in.  But as Peter 
says, if it's mutable, you then open yourself to somebody changing them 
all, thinking it was only changing the one.  But there are good reasons 
not to use this trick to save memory.



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


Re: Re-using copyrighted code

2013-06-10 Thread Mark Janssen
> Weird Al can be a complex case, because sometimes his songs are true 
> parodies, and sometimes they're more satires.  Parody has a pretty firm 
> history of being protected under fair use, and Weird Al's MJ-inspired songs 
> ("Fat" and "Eat It") are clearly parodies.  (As is his more recent Lady Gaga 
> sendup "Perform This Way", while his Star wars saga "The Story Begins" and 
> Coolio-esque "Amish Paradise" are more like satires).
>
> So in the case of Weird Al's Michael Jackson parodies, he would be protected 
> under law if MJ had decided to sue.

Not entirely.  The use of the musical tune is not a parody, only the
lyrics.  But if, like you say, he did get permission, then he is safe.

But you bring up a point of *criticism* which is distinct from re-use.
-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Popen and reading stdout in windows

2013-06-10 Thread Dave Angel

On 06/10/2013 02:37 PM, Joseph L. Casale wrote:

I have a use where writing an interim file is not convenient and I was hoping to
iterate through maybe 100k lines of output by a process as its generated or
roughly anyways.

Seems to be a common question on ST, and more easily solved in Linux.
Anyone currently doing this with Python 2.7 in windows and can share some
guidance?



You leave out an awful amount of detail.  I have no idea what ST is, so 
I'll have to guess your real problem.


You've got a process (myprog.exe) which generates a medium amount of 
output to stdout, and you want to process that data in a python program 
as it's being output, but without first writing it to a file.


If by process you meant grep, the answer would be as simple as

myprocess | grep  parm1 parm2

But you want to write something (not called grep) in Python.

myprocess | python myfilter.py

The question is how to write myfilter.py


Answer is to use stdin as you would a file.  it's already open for you, 
and it'll get the data as it's being generated (plus or minus some 
buffering).  So you can simply do something like:




import sys
for index, line in enumerate(sys.stdin):
print index, line


This trivial "filter" adds a line number in front of every line.  But 
you could do anything there.  And you might not need enumerate if you 
don't care about the line number.


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


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread jmfauth

-

A coding scheme works with three sets. A *unique* set
of CHARACTERS, a *unique* set of CODE POINTS and a *unique*
set of ENCODED CODE POINTS, unicode or not.

The relation between the set of characters and the set of the
code points is a *human* table, created with a sheet of paper
and a pencil, a deliberate choice of characters with integers
as "labels".

The relation between the set of the code points and the
set of encoded code points is a "mathematical" operation.

In the case of an "8bits" coding scheme, like iso-XXX,
this operation is a no-op, the relation is an identity.
Shortly: set of code points == set of encoded code points.

In the case of unicode, The Unicode consortium endorses
three such mathematical operations called UTF-8, UTF-16 and
UTF-32 where UTF means Unicode Transformation Format, a
confusing wording meaning at the same time, the process
and the result of the process. This Unicode Transformation does
not produce bytes, it produces words/chunks/tokens of *bits* with
lengths 8, 16, 32, called Unicode Transformation Units (from this
the names UTF-8, -16, -32). At this level, only a structure has
been defined (there is no computing). Very important, an healthy
coding scheme works conceptually only with this *unique" set
of encoded code points, not with bytes, characters or code points.

The last step, the machine implementation: it is up to the
processor, the compiler, the language to implement all these
Unicode Transformation Units with of course their related
specifities: char, w_char, int, long, endianess, rune (Go
language), ...

Not too over-simplified or not too over-complicated and enough
to understand one, if not THE, design mistake of the flexible
string representation.

jmf

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Terry Jan Reedy

On 6/10/2013 12:09 PM, Rui Maciel wrote:


We've established that you don't like attribute declarations, at least those
you describe as not fulfill a technical purpose.  What I don't understand is
why you claim that that would "cause nothing but trouble".


Three answers:
Look how much trouble it has already caused ;-)
Since you are a self-declared newbie, believe us!
Since, be definition, useless code can do no good, it can only cause 
trouble. Think about it.


Terry


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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Terry Jan Reedy

On 6/10/2013 9:18 AM, Rui Maciel wrote:


class Model:
 points = []
 lines = []


Unless you actually need keep the points and lines ordered by entry 
order, or expect to keep sorting them by whatever, sets may be better 
than lists. Testing that a point or line is in the model will be faster, 
as will deleting one by its identify.



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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Ian Kelly
On Mon, Jun 10, 2013 at 7:57 AM, Rui Maciel  wrote:
> # Case A: this works
> model.points[0].position = [2,3,4]
> line.points
>
> # Case B: this doesn't work
> test.model.points[0] = test.Point(5,4,7)
> line.points
> 
>
>
> Is there a Python way of getting the same effect with Case B?


It's informative to think about what object is actually being mutated
in each of those examples.  In A, you are mutating the Point instance
by replacing its "position" attribute with a new list.  Since the Line
object references the same Point instance, it "sees" the change.  In
B, you are actually mutating a list *containing* the Point instance,
by replacing the Point in the list with some new Point.  The replaced
Point itself is not changed at all (apart from having one fewer
reference), and so from the perspective of the Line nothing has
changed.

There are a couple of ways you might get this to work the way you
want.  One is by adding as an extra layer a proxy object, which could
be as simple as:

class Proxy(object):
def __init__(self, ref):
self.ref = ref

Instead of adding points to the model, add instances of Proxy that
contain the points.  When adding points to the lines, add the same
Proxy objects instead.  Later, when you want to replace a point in the
model, leave the Proxy in place but change the Point it contains by
modifying the "ref" attribute.  Since the Line only directly
references the Proxy, it must follow the same ref attribute to access
the Point, and so in that way it will "see" the change.  The downsides
to this approach are that you have to then use the Proxy objects all
over the place and explicitly "dereference" them by using the ref
attribute all over the place.

Another possibility is to subclass the list used to store the points,
in order to modify the way that it replaces items.  That could look
something like this:

class UpdateList(list):
def __setitem__(self, index, item):
if isinstance(index, slice):
indices = range(* index.indices(len(self)))
items = list(item)
if len(indices) != len(items):
raise TypeError("Slice length does not match sequence length")
for i, x in zip(indices, items):
self[i].update(x)
else:
self[index].update(item)

Then you just need to add an "update" method to the Point class (and
any other class you might want to use this with) that would update the
Point's attributes to match those of another point.  The downside here
is that this is not very Pythonic, in that somebody familiar with
Python who is reading the code would be surprised by the way the model
updates work.

A third possibility of course would be to just not try to do case B.

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


Re: Re-using copyrighted code

2013-06-10 Thread Chris Angelico
On Tue, Jun 11, 2013 at 5:40 AM, Mark Janssen  wrote:
>> Weird Al can be a complex case, because sometimes his songs are true 
>> parodies, and sometimes they're more satires.  Parody has a pretty firm 
>> history of being protected under fair use, and Weird Al's MJ-inspired songs 
>> ("Fat" and "Eat It") are clearly parodies.  (As is his more recent Lady Gaga 
>> sendup "Perform This Way", while his Star wars saga "The Story Begins" and 
>> Coolio-esque "Amish Paradise" are more like satires).
>>
>> So in the case of Weird Al's Michael Jackson parodies, he would be protected 
>> under law if MJ had decided to sue.
>
> Not entirely.  The use of the musical tune is not a parody, only the
> lyrics.  But if, like you say, he did get permission, then he is safe.

Citing once again Gilbert and Sullivan, it's definitely possible for a
tune to be a parody. Compare "Poor Wand'ring One" from G&S's Pirates
of Penzance with "Sempre Libera" from Verdi's La Traviata - the former
is most definitely a parody of the latter. (And the song name is
reminiscent of the opera name, too.) There are other parodies in
Gilbert and Sullivan, of both lyrical and musical forms; sometimes
both, like when a set of warriors take off their armor before a fight,
set to music similar to that used in Handel's works for warriors
*putting on* armor.

There's plenty of room to make direct or indirect references in music.
Sometimes all it takes is a bar or two, and everyone knows what you're
parodying. That's even tighter than words!

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Terry Jan Reedy wrote:

> On 6/10/2013 9:18 AM, Rui Maciel wrote:
> 
>> class Model:
>>  points = []
>>  lines = []
> 
> Unless you actually need keep the points and lines ordered by entry
> order, or expect to keep sorting them by whatever, sets may be better
> than lists. Testing that a point or line is in the model will be faster,
> as will deleting one by its identify.

Thanks for the tip, Terry.  Sounds great.  I'll update my code.


Once again, thanks for the help,
Rui Maciel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Terry Jan Reedy wrote:

> Three answers:
> Look how much trouble it has already caused ;-)
> Since you are a self-declared newbie, believe us!
> Since, be definition, useless code can do no good, it can only cause
> trouble. Think about it.

I don't doubt that there might good reasons for that, but it is always 
preferable to get the rationale behind a decision to be able to understand 
how things work and how to do things properly.


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


Re: Changing filenames from Greeklish => Greek (subprocess complain)

2013-06-10 Thread Ned Batchelder
On Monday, June 10, 2013 3:48:08 PM UTC-4, jmfauth wrote:
> -
> 
> 
> 
> A coding scheme works with three sets. A *unique* set
> of CHARACTERS, a *unique* set of CODE POINTS and a *unique*
> set of ENCODED CODE POINTS, unicode or not.
> 
> The relation between the set of characters and the set of the
> code points is a *human* table, created with a sheet of paper
> and a pencil, a deliberate choice of characters with integers
> as "labels".
> 
> The relation between the set of the code points and the
> set of encoded code points is a "mathematical" operation.
> 
> In the case of an "8bits" coding scheme, like iso-XXX,
> this operation is a no-op, the relation is an identity.
> Shortly: set of code points == set of encoded code points.
> 
> In the case of unicode, The Unicode consortium endorses
> three such mathematical operations called UTF-8, UTF-16 and
> UTF-32 where UTF means Unicode Transformation Format, a
> confusing wording meaning at the same time, the process
> and the result of the process. This Unicode Transformation does
> not produce bytes, it produces words/chunks/tokens of *bits* with
> lengths 8, 16, 32, called Unicode Transformation Units (from this
> the names UTF-8, -16, -32). At this level, only a structure has
> been defined (there is no computing). 

This is a really good description of the issues involved with character sets 
and encodings, thanks.

> Very important, an healthy
> coding scheme works conceptually only with this *unique" set
> of encoded code points, not with bytes, characters or code points.
> 

You don't explain why it is important to work with encoded code points.  What's 
wrong with working with code points?

> 
> The last step, the machine implementation: it is up to the
> processor, the compiler, the language to implement all these
> Unicode Transformation Units with of course their related
> specifities: char, w_char, int, long, endianess, rune (Go
> language), ...
> 
> Not too over-simplified or not too over-complicated and enough
> to understand one, if not THE, design mistake of the flexible
> string representation.
> 
> jmf

Again you've made the claim that the flexible string representation is a 
mistake.  But you haven't said WHY.  I can't tell if you are trolling us, or 
are deluded, or genuinely don't understand what you are talking about.

Some day you might explain yourself. I look forward to it.

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Rui Maciel
Dave Angel wrote:

> So why do you also have an instance attribute of the same name?

Thanks to this thread, and after a bit of reading, I've finally managed to 
discover that in Python there are class attributes and instance attributes, 
the former working similarly to C++'s static member variables and the latter 
being more like proper member variables.

And there was light.

Python.org's tutorial could cover this issue a bit better than it does.


Thanks for the help,
Rui Maciel
-- 
http://mail.python.org/mailman/listinfo/python-list


Split a list into two parts based on a filter?

2013-06-10 Thread Roy Smith
I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list.  I could do:

new_songs = []
old_songs = []
for s in songs:
if s.is_new():
new_songs.append(s)
else:
old_songs.append(s)

Which works, but is klunky compared to the two-liner above.  This
seems like a common enough thing that I was expecting to find
something in itertools which did this.  I'm thinking something along
the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re-using copyrighted code

2013-06-10 Thread llanitedave
On Monday, June 10, 2013 12:40:57 PM UTC-7, zipher wrote:
> > Weird Al can be a complex case, because sometimes his songs are true 
> > parodies, and sometimes they're more satires.  Parody has a pretty firm 
> > history of being protected under fair use, and Weird Al's MJ-inspired songs 
> > ("Fat" and "Eat It") are clearly parodies.  (As is his more recent Lady 
> > Gaga sendup "Perform This Way", while his Star wars saga "The Story Begins" 
> > and Coolio-esque "Amish Paradise" are more like satires).
> 
> >
> 
> > So in the case of Weird Al's Michael Jackson parodies, he would be 
> > protected under law if MJ had decided to sue.
> 
> 
> 
> Not entirely.  The use of the musical tune is not a parody, only the
> 
> lyrics.  But if, like you say, he did get permission, then he is safe.
> 
> 
> 
> But you bring up a point of *criticism* which is distinct from re-use.
> 
> -- 
> 
> MarkJ
> 
> Tacoma, Washington


In this case, the tune and the lyrics really aren't separable.  What's being 
parodied is the entire work, including the music video, down to the costumes, 
the dance moves, and the guitar solo.  It's the work, taken as a whole.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py_compile vs. built-in compile, with __future__

2013-06-10 Thread Terry Jan Reedy

On 6/10/2013 11:33 AM, dhyams wrote:

The built-in compile() function has a "flags" parameter that one can
use to influence the "__future__" mechanism. However,
py_compile.compile, which I'm using to byte-compile code, doesn't
have an equivalent means to do this.


That flag was added to compile bacause it is needed to compile 
expressions and single statements, whether in string or ast form, that 
use future syntax. It is impossible to include a future statement with 
either. It is not needed for compiling multiple statements.



Is this by design, or would this be considered a bug?


Design, not needed.


import __future__
py_compile.compile("foobar.py",flags=__future__.CO_FUTURE_DIVISION)


Put the future statement inside foobar.py just as you would do if 
running it from the command line. Notice that there is no command-line 
future flag either.


Terry



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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Terry Jan Reedy

On 6/10/2013 4:13 PM, Rui Maciel wrote:

Terry Jan Reedy wrote:


Three answers:
Look how much trouble it has already caused ;-)
Since you are a self-declared newbie, believe us!
Since, be definition, useless code can do no good, it can only cause
trouble. Think about it.


I don't doubt that there might good reasons for that, but it is always
preferable to get the rationale behind a decision to be able to understand
how things work and how to do things properly.


I agree actually. But sometimes is it hard to articulate 'good reasons' 
for a principle based on the integration of over a decade of experience. 
I was really trying to point to the difference between


'I will not accept the experience-based advice until enough good reasons 
are presented.' and


'I will provisionally accept the advice but I would still like to know why.'

Another principle similar to 'Don't add extraneous code' is 'Don't 
rebind builtins*'. I have a separate post for that.


Terry



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


Re: Encoding questions (continuation)

2013-06-10 Thread Lele Gaifax
Steven D'Aprano  writes:

>> I did but docs confuse me even more. Can you pleas ebut it simple.
>
> Nikos, if you can't be bothered to correct your spelling mistakes, why 
> should we be bothered to answer your questions?

Maybe he just want to prove we are smart enough...

http://www.foxnews.com/story/0,2933,511177,00.html

Or maybe his encoding algorithm needs some refinement
:-)

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: py_compile vs. built-in compile, with __future__

2013-06-10 Thread dhyams
On Monday, June 10, 2013 4:59:35 PM UTC-4, Terry Jan Reedy wrote:
> On 6/10/2013 11:33 AM, dhyams wrote:
> 
> > The built-in compile() function has a "flags" parameter that one can
> 
> > use to influence the "__future__" mechanism. However,
> 
> > py_compile.compile, which I'm using to byte-compile code, doesn't
> 
> > have an equivalent means to do this.
> 
> 
> 
> That flag was added to compile bacause it is needed to compile 
> 
> expressions and single statements, whether in string or ast form, that 
> 
> use future syntax. It is impossible to include a future statement with 
> 
> either. It is not needed for compiling multiple statements. 
> > Is this by design, or would this be considered a bug?
> 
> 
> 
> Design, not needed.
> 
> 
> 
> > import __future__
> 
> > py_compile.compile("foobar.py",flags=__future__.CO_FUTURE_DIVISION)
> 
> 
> 
> Put the future statement inside foobar.py just as you would do if 
> 
> running it from the command line. Notice that there is no command-line 
> 
> future flag either.
> 
> 
> 
> Terry


I guess I'll have to agree to disagree here...the situation I'm in is that I 
want a user to be able to write a mathematical plugin with as little effort as 
possible.  So I want the "from __future__ import division" to be baked into the 
plugin, without have to require the user to put that bit of confusingness at 
the top of every plugin they write.  It's a matter of elegance to the end-user, 
especially because I want to make the plugins as idiot-proof as I can.  It will 
be common for a user not familiar with python to make the common 1/2 mistake 
(vs. 1.0/2.0).

Is that not a reasonable use-case?

I added the capability in my local Python tree with two very small 
modifications to py_compile.py:


Change (I'm leaving out the line numbers because they will be different for 
different versions of Python).

< def compile(file, cfile=None, dfile=None, doraise=False):
> def compile(file, cfile=None, dfile=None, doraise=False, flags=0):

and

< codeobject = __builtin__.compile(codestring, dfile or file,'exec')
> codeobject = __builtin__.compile(codestring, dfile or file,'exec',flags=flags)

Seems like a reasonable use-case and a correspondingly tiny change to the 
Python source code base to me...but if no one else sees the value, then I'll 
just leave it alone.


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


Re: py_compile vs. built-in compile, with __future__

2013-06-10 Thread Chris Angelico
On Tue, Jun 11, 2013 at 8:27 AM, dhyams  wrote:
> I guess I'll have to agree to disagree here...the situation I'm in is that I 
> want a user to be able to write a mathematical plugin with as little effort 
> as possible.  So I want the "from __future__ import division" to be baked 
> into the plugin, without have to require the user to put that bit of 
> confusingness at the top of every plugin they write.  It's a matter of 
> elegance to the end-user, especially because I want to make the plugins as 
> idiot-proof as I can.  It will be common for a user not familiar with python 
> to make the common 1/2 mistake (vs. 1.0/2.0).
>
> Is that not a reasonable use-case?

Can you read the file into a string, prepend a future directive, and
then compile the string?

Alternatively, can you switch to Python 3, where the future directive
isn't necessary? :)

If all else fails, you should be able to just copy and mod the
function into your own source file.

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Grant Edwards
On 2013-06-10, Terry Jan Reedy  wrote:

> Another principle similar to 'Don't add extraneous code' is 'Don't 
> rebind builtins'.

OK, we've all done it by accident (especially when starting out), but
are there people that rebind builtins intentionally?

-- 
Grant Edwards   grant.b.edwardsYow! FROZEN ENTREES may
  at   be flung by members of
  gmail.comopposing SWANSON SECTS ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Roel Schroeven

Roy Smith schreef:

I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list.  I could do:

new_songs = []
old_songs = []
for s in songs:
if s.is_new():
new_songs.append(s)
else:
old_songs.append(s)

Which works, but is klunky compared to the two-liner above.  This
seems like a common enough thing that I was expecting to find
something in itertools which did this.  I'm thinking something along
the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?


You could do something like:

new_songs, old_songs = [], []
[(new_songs if s.is_new() else old_songs).append(s) for s in songs]

But I'm not sure that that's any better than the long version.

--
"People almost invariably arrive at their beliefs not on the basis of
proof but on the basis of what they find attractive."
-- Pascal Blaise

r...@roelschroeven.net

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


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Chris Angelico
On Tue, Jun 11, 2013 at 6:34 AM, Roy Smith  wrote:
> new_songs = [s for s in songs if s.is_new()]
> old_songs = [s for s in songs if not s.is_new()]

Hmm. Would this serve?

old_songs = songs[:]
new_songs = [songs.remove(s) or s for s in songs if s.is_new()]

Python doesn't, AFAIK, have a "destructive remove and return"
operation, and del is a statement rather than an expression/operator,
but maybe this basic idea could be refined into something more useful.
It guarantees to call is_new only once per song.

The iterator version strikes my fancy. Maybe this isn't of use to you,
but I'm going to try my hand at making one anyway.

>>> def iterpartition(pred,it):
"""Partition an iterable based on a predicate.

Returns two iterables, for those with pred False and those True."""
falses,trues=[],[]
it=iter(it)
def get_false():
while True:
if falses: yield falses.pop(0)
else:
while True:
val=next(it)
if pred(val): trues.append(val)
else: break
yield val
def get_true():
while True:
if trues: yield trues.pop(0)
else:
while True:
val=next(it)
if not pred(val): falses.append(val)
else: break
yield val
return get_false(),get_true()
>>> f,t=iterpartition(lambda x: x%3,range(1))
>>> next(t)
1
>>> next(t)
2
>>> next(t)
4
>>> next(t)
5
>>> next(f)
0
>>> next(f)
3
>>> next(f)
6
>>> next(f)
9
>>> next(f)
12
>>> next(t)
7
>>> next(t)
8

Ha. :) Useless but fun.

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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Chris Angelico
On Tue, Jun 11, 2013 at 8:39 AM, Grant Edwards  wrote:
> On 2013-06-10, Terry Jan Reedy  wrote:
>
>> Another principle similar to 'Don't add extraneous code' is 'Don't
>> rebind builtins'.
>
> OK, we've all done it by accident (especially when starting out), but
> are there people that rebind builtins intentionally?

There are times when you don't care what you shadow, like using id for
a database ID.

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


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Chris Rebert
On Mon, Jun 10, 2013 at 1:34 PM, Roy Smith  wrote:
> I have a list, songs, which I want to divide into two groups.
> Essentially, I want:
>
> new_songs = [s for s in songs if s.is_new()]
> old_songs = [s for s in songs if not s.is_new()]
>
> but I don't want to make two passes over the list.  I could do:
>
> new_songs = []
> old_songs = []
> for s in songs:
> if s.is_new():
> new_songs.append(s)
> else:
> old_songs.append(s)
>
> Which works, but is klunky compared to the two-liner above.  This
> seems like a common enough thing that I was expecting to find
> something in itertools which did this.  I'm thinking something along
> the lines of:
>
> matches, non_matches = isplit(lambda s: s.is_new, songs)
>
> Does such a thing exist?

itertools.groupby() is kinda similar, but unfortunately doesn't fit
the bill due to its sorting requirement.
There is regrettably no itertools.partition(). And given how dead-set
Raymond seems to be against adding things to the itertools module,
there will likely never be.
Maybe more-itertools (https://pypi.python.org/pypi/more-itertools )
would accept a patch?

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


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Tim Chase
On 2013-06-11 08:50, Chris Angelico wrote:
> The iterator version strikes my fancy. Maybe this isn't of use to
> you, but I'm going to try my hand at making one anyway.
> 
> >>> def iterpartition(pred,it):
>   """Partition an iterable based on a predicate.
> 
>   Returns two iterables, for those with pred False and those
> True.""" falses,trues=[],[]

This is really nice.  I think the only major modification I'd make is
to use a collections.deque() instead of lists here:

  trues, falses = collections.deque(), collections.deque()

as I seem to recall that list.pop(0) has some performance issues if
it gets long, while the deque is optimized for fast (O(1)?) push/pop
on either end.

-tkc



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


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Chris Angelico
On Tue, Jun 11, 2013 at 9:10 AM, Tim Chase
 wrote:
> On 2013-06-11 08:50, Chris Angelico wrote:
>> The iterator version strikes my fancy. Maybe this isn't of use to
>> you, but I'm going to try my hand at making one anyway.
>>
>> >>> def iterpartition(pred,it):
>>   """Partition an iterable based on a predicate.
>>
>>   Returns two iterables, for those with pred False and those
>> True.""" falses,trues=[],[]
>
> This is really nice.  I think the only major modification I'd make is
> to use a collections.deque() instead of lists here:
>
>   trues, falses = collections.deque(), collections.deque()
>
> as I seem to recall that list.pop(0) has some performance issues if
> it gets long, while the deque is optimized for fast (O(1)?) push/pop
> on either end.

Sure. If it's that good I might submit it to more-itertools... heh.

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


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 23:54, "Roel Schroeven"  wrote:
>
> You could do something like:
>
> new_songs, old_songs = [], []
> [(new_songs if s.is_new() else old_songs).append(s) for s in songs]
>
> But I'm not sure that that's any better than the long version.

This is so beautiful!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Tim Chase
On 2013-06-11 08:54, Chris Angelico wrote:
> >> Another principle similar to 'Don't add extraneous code' is
> >> 'Don't rebind builtins'.
> >
> > OK, we've all done it by accident (especially when starting out),
> > but are there people that rebind builtins intentionally?
> 
> There are times when you don't care what you shadow, like using id
> for a database ID.

While that's certainly the most common, there are a lot of items in
__builtins__ that are there for convenience that I wouldn't think
twice about rebinding, especially in a local scope (I might take more
care at a module scope, but still wouldn't care much).  E.g.

  apply
  bin
  buffer
  coerce
  filter
  format
  input

Some are deprecated, some are easily replaced by (what I consider
more readible) list comprehensions, and some have alternate meanings
that might be the right word-choice inside a function and wouldn't be
missed (I'd expect to see a bin-sort function use variables called
"bin" and not needing to convert from integer-to-string)

And some strings that are harmless outside the interactive interpreter

  copyright
  credits
  exit
  license
  quit

-tkc






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


Re: Newbie: question regarding references and class relationships

2013-06-10 Thread Dave Angel

On 06/10/2013 06:54 PM, Chris Angelico wrote:

On Tue, Jun 11, 2013 at 8:39 AM, Grant Edwards  wrote:

On 2013-06-10, Terry Jan Reedy  wrote:


Another principle similar to 'Don't add extraneous code' is 'Don't
rebind builtins'.


OK, we've all done it by accident (especially when starting out), but
are there people that rebind builtins intentionally?


There are times when you don't care what you shadow, like using id for
a database ID.

ChrisA



And times where you're deliberately replacing a built-in

try:
   input = raw_input
except 



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


Re: py_compile vs. built-in compile, with __future__

2013-06-10 Thread dhyams
On Monday, June 10, 2013 6:36:04 PM UTC-4, Chris Angelico wrote:
> On Tue, Jun 11, 2013 at 8:27 AM, dhyams  wrote:
> 
> > I guess I'll have to agree to disagree here...the situation I'm in is that 
> > I want a user to be able to write a mathematical plugin with as little 
> > effort as possible.  So I want the "from __future__ import division" to be 
> > baked into the plugin, without have to require the user to put that bit of 
> > confusingness at the top of every plugin they write.  It's a matter of 
> > elegance to the end-user, especially because I want to make the plugins as 
> > idiot-proof as I can.  It will be common for a user not familiar with 
> > python to make the common 1/2 mistake (vs. 1.0/2.0).
> 
> >
> 
> > Is that not a reasonable use-case?
> 
> 
> 
> Can you read the file into a string, prepend a future directive, and
> 
> then compile the string?

Technically yes, except that now there is complication of writing the modified 
module back to a file so that I can still use py_compile.compile() to byte 
compile that code.  If I don't do that, then I would be duplicating the work of 
py_compile.compile(), which wouldn't be good design.  And I some file caching 
already going on that is reasonably complicated already, that I didn't want to 
add another level of failure modes to.


 
> Alternatively, can you switch to Python 3, where the future directive
> 
> isn't necessary? :)

If only all of my dependencies were Python 3 ready ;)  But that really doesn't 
solve the underlying problem...surely there will be other "futures" that people 
will want to use when moving from Python 3.x to Python y.z, for example.

> 
> If all else fails, you should be able to just copy and mod the
> 
> function into your own source file.

That's kind of where I am now, but IMO the small addition of the flags argument 
to py_compile.compile() takes care of things.


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


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Peter Otten
Chris Angelico wrote:

> On Tue, Jun 11, 2013 at 6:34 AM, Roy Smith  wrote:
>> new_songs = [s for s in songs if s.is_new()]
>> old_songs = [s for s in songs if not s.is_new()]
> 
> Hmm. Would this serve?
> 
> old_songs = songs[:]
> new_songs = [songs.remove(s) or s for s in songs if s.is_new()]
> 
> Python doesn't, AFAIK, have a "destructive remove and return"
> operation, and del is a statement rather than an expression/operator,
> but maybe this basic idea could be refined into something more useful.
> It guarantees to call is_new only once per song.
> 
> The iterator version strikes my fancy. Maybe this isn't of use to you,
> but I'm going to try my hand at making one anyway.

> >>> def iterpartition(pred,it):
> """Partition an iterable based on a predicate.
> 
> Returns two iterables, for those with pred False and those 
True."""
> falses,trues=[],[]
> it=iter(it)
> def get_false():
> while True:
> if falses: yield falses.pop(0)
> else:
> while True:
> val=next(it)
> if pred(val): trues.append(val)
> else: break
> yield val
> def get_true():
> while True:
> if trues: yield trues.pop(0)
> else:
> while True:
> val=next(it)
> if not pred(val): 
falses.append(val)
> else: break
> yield val
> return get_false(),get_true()

An alternative implementation, based on itertools.tee:

import itertools

def partition(items, predicate=bool):
a, b = itertools.tee((predicate(item), item) for item in items)
return ((item for pred, item in a if not pred),
(item for pred, item in b if pred))

if __name__ == "__main__":
false, true = partition(range(10), lambda item: item % 2)
print(list(false))
print(list(true))

def echo_odd(item):
print("checking", item)
return item % 2

false, true = partition(range(10), echo_odd)

print("FALSE", [next(false) for _ in range(3)])
print("TRUE", next(true))
print("FALSE", list(false))
print("TRUE", list(true))


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


"Don't rebind built-in names*" - it confuses readers

2013-06-10 Thread Terry Jan Reedy

Many long-time posters have advised "Don't rebind built-in names*.

* Unless you really mean to mask it, or more likely wrap it, such as 
wrapping print to modify some aspect of its operation than one cannot do 
with its keyword parameters. The point for this post is that such 
wrapping modify or extend the basic meaning of the builtin, but do not 
abolish it.


Reasons have been given in various related forms: 'my long experience 
tells me its bad', 'you may need the builtin later', 'you may forget 
that you rebound the builtin, 'it can lead to subtle bugs, etc.


Leaving aside the code writer and code operation, I recently discovered 
that it is not nice for readers, whether humans or programs.


For instance, open Lib/idlelib/GrepDialog.py in an editor that colorizes 
Python syntax, such as Idle's editor, jump down to the bottom and read 
up, and (until it is patched) find

list.append(fn)
with 'list' colored as a builtin. Stop. That looks wrong. List.append 
needs two arguments: a list instance and an object to append to the 
list. The 'solution' is in a previous line

list = []
Reading further, one sees that the function works with two lists, a list 
of file names, unfortunately called 'list', and a list of 
subdirectories, more sensibly call 'subdirs'. I was initially confused 
and reading the code still takes a small bit of extra mental energy. 
Idle stays confused and will wrongly color the list instance name until 
it is changed. Calling the file list 'fnames' or 'filenames' would have 
been clearer to both me and Idle.


--
Terry Jan Reedy

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


Re: "Don't rebind built-in names*" - it confuses readers

2013-06-10 Thread Mark Janssen
>> list = []
>> Reading further, one sees that the function works with two lists, a list of
>> file names, unfortunately called 'list',
>
> That is very good advice in general:  never choose a variable name
> that is a keyword.

Btw,  shouldn't it be illegal anyway?  Most compilers don't let you do
use a keyword as a variable name

-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Don't rebind built-in names*" - it confuses readers

2013-06-10 Thread Tim Chase
On 2013-06-10 17:20, Mark Janssen wrote:
> >> list = []
> >> Reading further, one sees that the function works with two
> >> lists, a list of file names, unfortunately called 'list',
> >
> > That is very good advice in general:  never choose a variable name
> > that is a keyword.
> 
> Btw,  shouldn't it be illegal anyway?  Most compilers don't let you
> do use a keyword as a variable name

There's a subtle difference between a keyword and a built-in.  Good
Python style generally avoids masking built-ins but allows it:

  >>> "file" in dir(__builtins__)
  True
  >>> file = "hello" # bad style, but permitted
  >>> print file
  hello

Whereas the compiler prevents you from tromping on actual keywords:

  >>> for = 4
File "", line 1
  for = 4
  ^
  SyntaxError: invalid syntax

-tkc




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


Re: "Don't rebind built-in names*" - it confuses readers

2013-06-10 Thread Mark Janssen
> There's a subtle difference between a keyword and a built-in.  Good
> Python style generally avoids masking built-ins but allows it:

Right, thank you for reminding me.  My C-mind put them in the same category.
-- 
MarkJ
Tacoma, Washington
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >