Re: [Tutor] regex: matching unicode

2012-12-23 Thread Albert-Jan Roskam
>>Is the code below the only/shortest way to match unicode characters? I would 
>>like to match whatever is defined as a character in the unicode reference 
>>database. So letters in the broadest sense of the word, but not digits, 
>>underscore or whitespace. Until just now, I was convinced that the re.UNICODE 
>>flag generalized the [a-z] class to all unicode letters, and that the absence 
>>of re.U was an implicit 're.ASCII'. Apparently that mental model was *wrong*.

>>But [^\W\s\d_]+ is kind of hard to read/write.
>>
>>import re
>>s = unichr(956)  # mu sign
>>m = re.match(ur"[^\W\s\d_]+", s, re.I | re.U)
>>
>>
>A thought would be to rely on the general category of the character, as listed 
>in the Unicode database. Unicodedata.category will give you what you need. 
>Here is a list of categories in the Unicode standard:
>
>
>http://www.fileformat.info/info/unicode/category/index.htm
>
>
>
>So, if you wanted only letters, you could say:
>
>
>def is_unicode_character(c):
>    assert len(c) == 1
>    return 'L' in unicodedata.category(c)


Hi everybody,

Thanks for your replies, they have been most insightful. For now the 
'unicodedata' approach works best for me. I need to validate a word and this is 
now a two-step approach. First, check if the first character is a (unicode) 
letter, second, do other checks with a regular regex (ie., no spaces, 
ampersands and whatnot). Using one regex would be more elegant though, but I 
got kinda intimidated by the hail of additional flags in the regex module.
Having unicode versions of the classes \d, \w, etc (let's call them \ud, \uw) 
would be cool.Here another useful way to use your (Hugo's) function. The 
Japanese hangul sign and the degree sign almost look the same!

import unicodedata

hangul = unichr(4363)
degree = unichr(176)

def isUnicodeChar(c):
  assert len(c) == 1
  c = c.decode("utf-8") if isinstance(c, str) else c
  return 'L' in unicodedata.category(c)

>>> isUnicodeChar(hangul)
True
>>> isUnicodeChar(degree)
False
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of int() in converting strings

2012-12-23 Thread eryksun
On Sat, Dec 22, 2012 at 12:57 PM, Oscar Benjamin
 wrote:
>>>
>>> def make_int(obj):
>>>  '''Coerce str, float and int to int without rounding error
>>>  Accepts strings like '4.0' but not '4.1'
>>>  '''
>>>  fnum = float('%s' % obj)
>>>  inum = int(fnum)
>>>  assert inum == fnum
>>>  return inum
>>
>> Well, that function is dangerously wrong. In no particular order,
>> I can find four bugs and one design flaw.
>
> I expected someone to object to this function. I had hoped that they
> might also offer an improved version, though. I can't see a good way
> to do this without special casing the treatment of some or other type
> (the obvious one being str).


Strings don't implement __int__ or __trunc__; they aren't numbers, so
why not special case them? You can parse strings with obj =
Decimal(obj) (this uses a regex). Then for all inputs set inum =
int(obj) and raise ValueError if inum != obj.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For Loop Question

2012-12-23 Thread Dave Angel
On 12/23/2012 09:02 PM, Alan Gauld wrote:
> On 24/12/12 01:16, Mitya Sirenef wrote:
>> On 12/23/2012 08:03 PM, Tom Tucker wrote:
>>>
>>>
>>> Python Gurus,
>>> I got a question for ya.  Below I have three instance variables
>>> (self.A, self.B, etc).  How can I use the below
>>> for loop for A, B, C to also call those instance variables?
>>>
>
> Your example is not entirely clear.
>
> You do realize that self is only defined inside a method? Therefore,
> by implication, all of your code should lie inside a class and most
> of it inside a single method? Therefore, I'd expect your code
> to look like:
>
>
>>> 
>>> .
>>> .
> class MyClass:
>   def someMethod(self):
>>>  self.A   = 1
>>>  self.B   = 2
>>>  self.C   = 3
>>>
>   def someOtherMethod(self):
>>> myDict = {'A': 1, 'B': 2, 'C': 1}
>>> for x in [A, B, C]:
>
But A, B, and C have no values yet.  Perhaps the OP means
for x in ["A", "B", "C"]

> in which case this becomes, as Mitya suggests:
>
>for key in myDict:
>   if myDict[key] == getattr(self, key):
>
> >   print "Yep, we have a match!"
> > else:
> >   print "Sorry..No match!"
>
> But, whatever you are trying to do here, there is very likely a better
> way to do it. We just don't know enough context to offer alternatives.
>
>
> HTH


-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex: matching unicode

2012-12-23 Thread eryksun
On Sat, Dec 22, 2012 at 11:12 PM, Steven D'Aprano  wrote:
>
> No. You could install a more Unicode-aware regex engine, and use it instead
> of Python's re module, where Unicode support is at best only partial.
>
> Try this one:
>
> http://pypi.python.org/pypi/regex

Looking over the old docs, I count 4 regex implementations up to 2.0:

regexp
regex (0.9.5)
re / pcre (1.5)
re / sre (2.0)

It would be nice to see Matthew Barnett's regex module added as an
option in 3.4, just as sre was added to 1.6 before taking the place of
pcre in 2.0.

> The failures are all numbers with category Nl or No ("letterlike
> numeric character" and "numeric character of other type").

The pattern basically matches any word character that's not a
decimal/underscore (the \s is redundant AFAIK). Any character that's
numeric but not decimal also matches. For example, the following are
all numeric:

\N{SUPERSCRIPT ONE}: category "No", digit, not decimal
\N{ROMAN NUMERAL ONE}: category "Nl", not digit, not decimal
\u4e00 (1, CJK): category "Lo", not digit, not decimal

Regarding the latter, if the pattern shouldn't match numeric
characters in a broad sense, then it should be OK to exclude CJK
numeric ideograms in category "Lo", but it's like excluding the word
"one".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For Loop Question

2012-12-23 Thread Alan Gauld

On 24/12/12 01:16, Mitya Sirenef wrote:

On 12/23/2012 08:03 PM, Tom Tucker wrote:



Python Gurus,
I got a question for ya.  Below I have three instance variables
(self.A, self.B, etc).  How can I use the below
for loop for A, B, C to also call those instance variables?



Your example is not entirely clear.

You do realize that self is only defined inside a method? Therefore,
by implication, all of your code should lie inside a class and most
of it inside a single method? Therefore, I'd expect your code
to look like:




.
.

class MyClass:
  def someMethod(self):

 self.A   = 1
 self.B   = 2
 self.C   = 3


  def someOtherMethod(self):

myDict = {'A': 1, 'B': 2, 'C': 1}
for x in [A, B, C]:


in which case this becomes, as Mitya suggests:

   for key in myDict:
  if myDict[key] == getattr(self, key):

>   print "Yep, we have a match!"
> else:
>   print "Sorry..No match!"

But, whatever you are trying to do here, there is very likely a better 
way to do it. We just don't know enough context to offer alternatives.



HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] For Loop Question

2012-12-23 Thread Mitya Sirenef

On 12/23/2012 08:03 PM, Tom Tucker wrote:



Python Gurus,
I got a question for ya.  Below I have three instance variables 
(self.A, self.B, etc).  How can I use the below

for loop for A, B, C to also call those instance variables?

Example
###

.
.
 self.A  = 1
 self.B   = 2
 self.C   = 3

myDict = {'A': 1, 'B': 2, 'C': 1}
for x in [A, B, C]:

  if myDict[x] == (self.%s % (x)): # Invalid if statement.. ;-)
print "Yep, we have a match!"
  else:
print "Sorry..No match!"





If I understood you right, you can do: myDict[x] == getattr(self, x)

 - mitya


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] For Loop Question

2012-12-23 Thread Tom Tucker
Python Gurus,
I got a question for ya.  Below I have three instance variables (self.A,
self.B, etc).  How can I use the below
for loop for A, B, C to also call those instance variables?

Example
###

.
.
 self.A  = 1
 self.B= 2
 self.C= 3

myDict = {'A': 1, 'B': 2, 'C': 1}
for x in [A, B, C]:

  if myDict[x] == (self.%s % (x)): # Invalid if statement.. ;-)
print "Yep, we have a match!"
  else:
print "Sorry..No match!"
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 106, Issue 60

2012-12-23 Thread Farrukh Ali
Thank you so so much Sir. Now i got u very well and have command on this 
concepty...


Regards




-- Message: 2 Date: Sat, 22 Dec 2012 
17:52:06 -0500 From: Dave Angel  To: tutor@python.org 
Subject: Re: [Tutor] check it for me Message-ID: 
<50d63996.9080...@davea.name> Content-Type: text/plain; 
charset=ISO-8859-1 On 12/22/2012 05:38 PM, Farrukh Ali wrote:

Hi, i am using ActivePython 2.7.2.5, and windows 8 professional.
well the original ex3.py is:
print "I will now count my chickens:"
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print "Now I will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
print "Is it true that 3 + 2 < 5 - 7?"
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

And i have made ex3.py this way below

print "I will now count my chickens:"
print "Hens",25+30.0/6
print "Roosters",100 - 25.0 * 3.0 % 4
print "Now I will count the eggs:"
print 3+2+1-5.0+4.0%2-1/4.0+6
print "Is it true that 3+2<5-7?"
print 3+2<5-7
print "What is 3+2?",3+2
print "What is 5-7?",5-7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5>-2
print "Is it greater or equal?", 5>=-2
print "Is it less or equal?", 5<=-2


The author has given us extra credit to practice so here it is and the
5th point below, which i was asking.

Extra Credit
1. Above each line, use the # to write a comment to yourself
explaining what the line does.
2. Remember in Exercise 0 when you started python? Start python this
way again and using the above characters
and what you know, use python as a calculator.
3. Find something you need to calculate and write a new .py file that
does it.
4. Notice the math seems "wrong"? There are no fractions, only whole
numbers. Find out why by researching
what a "floating point" number is.
5. Rewrite ex3.py to use floating point numbers so it's more accurate
(hint: 20.0 is floating point).

OK, so his point #4 is that for eggs, 1/4 gives a zero, not a 0.25.  So
you lose accuracy in that sense.  So point #5 is to allow fractions to
propagate.

Note that in Python 3.x,  1/4 will already give a float, rather than an
int, and if you want the 2.x behavior, you would write it as 1//4

Otherwise, it looks right to me.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Comprehension Syntax

2012-12-23 Thread Malcolm Newsome
> Message: 4
> Date: Sun, 23 Dec 2012 02:48:42 -0500
> From: Mario Cacciatore 
> To: "tutor@python.org" 
> Subject: [Tutor] List Comprehension Syntax
> Message-ID: <50d6b74d.031d650a.3497.a...@mx.google.com>
> Content-Type: text/plain; charset="windows-1252"
>
> Hey everyone,
>
> I am having a very hard time understanding the list comprehension syntax.
I've followed the docs and could use some guidance from the fine folks here
to supplement my findings. If someone wouldn't mind replying back with an
example or two, with some explanation of each part I'd appreciate it.
> -- next part --

Here's an example that I used a list comprehension for when I was trying to
learn it.

I'd leave it to the tutors to say whether this is an appropriate use of it.

# search sorted array for integer k

def finder(list, k):
try:
s = sorted([int(i) for i in list])
k = int(k)
except ValueError:
print "Args must be ints. Your list arg was: {0} and your k arg was
'{1}'." .format(list, k)
return None
if k in s:
print "Index is: {0}".format(s.index(k))
else:
return -1

Hope it helps!

Malcolm Newsome

P.S. I hope the formatting is ok...I'm responding from my phone.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Comprehension Syntax

2012-12-23 Thread Steven D'Aprano

On 23/12/12 18:48, Mario Cacciatore wrote:

Hey everyone,

I am having a very hard time understanding the list comprehension syntax.
I've followed the docs and could use some guidance from the fine folks
here to supplement my findings. If someone wouldn't mind replying back
with an example or two, with some explanation of each part I'd appreciate
it.



If you did mathematics in high school, you may remember set-builder notation:

http://en.wikipedia.org/wiki/Set-builder_notation

There are a number of variations of this notation, depending on how formal
you want to be. For example:

{3x+1 ∀ x ∈ {1, 2, 3}}

This says:

"build the set of values 3 times x plus 1, for all x values that are elements
of the set {1, 2, 3}"

and it would produce the values:

x=1 -> 3*1 + 1
x=2 -> 3*2 + 1
x=3 -> 3*3 + 1}

giving the final set {4, 7, 10}


Python uses similar notation, except based on English words instead of
mathematical symbols. In Python, we generally use lists or tuples, not sets,
so the list comprehension would be:

[3*x + 1 for x in (1, 2, 3)]


We can pull this apart to see what each part does.

[ ]

  The square brackets build a list.

3*x + 1

  This is the list comprehension expression. It is evaluated each time
  the list comp goes through the loop.

for x in (1, 2, 3)

  This sets up the list comprehension loop, and defines the loop
  variable, just like a for-loop. The tuple (1, 2, 3) is the loop
  sequence, x takes each value from this in turn.


So this list comprehension is equivalent to this for-loop:


tmp = []
for x in (1, 2, 3):
   tmp.append(3*x + 1)



except that you don't need to define a temporary list to accumulate the
results, the list comprehension does that for you.


List comprehensions can be more complicated. They can also take one or
more "if" clause:


[2*n for n in range(10) if n%2 == 1]


is equivalent to this for-loop:

tmp = []
for n in range(10):
if n%2 == 1:
tmp.append(2*n)



and so it will produce the list:

[2, 6, 10, 14, 18]


Naturally, you can use any sequence or iterable in the for-loop
part of list comps:

myname = "Quentin"
[c for c in myname if c.lower() != "q" if c.upper() != "T"]


will give

['u', 'e', 'i', 'n']


The sequence can even be another list comprehension:


[y+1 for y in [2*x for x in (1, 2, 3)] if y < 5]


gives [3, 5], and is equivalent to this pair of loops:


tmp1 = []
tmp2 = []
for x in (1, 2, 3):
tmp1.append(2*x)
for y in tmp1:
if y < 5:
tmp2.append(y+1)



List comps can also take multiple loops:

[(a, b) for a in range(3) for b in range(3)]


gives this result:

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]


and is equivalent to this nested loop:


tmp = []
for a in range(3):
for b in range(3):
tmp.append( (a, b) )



List comprehensions are powerful and compact, but because they are so compact,
they can also be hard to read. Resist the temptation to write every for-loop
as a list comprehension! Keep your list comps simple, and you will not regret
it. Nobody has ever said, "I wish my list comprehensions were more complicated
and hard to read!"



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Comprehension Syntax

2012-12-23 Thread Peter Otten
Mario Cacciatore wrote:

> Hey everyone,
> 
> I am having a very hard time understanding the list comprehension syntax.
> I've followed the docs and could use some guidance from the fine folks
> here to supplement my findings. If someone wouldn't mind replying back
> with an example or two, with some explanation of each part I'd appreciate
> it.

Consider

[c.lower() + x % 3 * c for x in range(10) if x not in [7,8] for c in "aBcDE" 
if c.isupper() if c != "B"]

That looks complex! So let's take it apart. Here's the cheat-sheet:

result = [expr(x) for x in items]

is equivalent to a for-loop:

result = []
for x in items:
result.append(expr(x))

The expression on the left of the list-comp moves into the (innermost if 
there is mor than one) loop.

result = [expr(x) for x in items if cond(x)]

is equivalent to

result = []
for x in items:
if cond(x):
result.append(expr(x))

You can add an arbitrary number of conditions:

result = [expr(x) for x in items if cond1(x) if cond2(x) if cond3(x)]

is equivalent to

result = []
for x in items:
if cond1(x):
if cond2(x):
if cond3(x):
result.append(expr(x))

You can also have multiple 'for' clauses:

result = [expr(x, y, z) for x in items1 for y in items2 for z in items3]

is equivalent to

result = []
for x in items1:
for y in items2:
for z in items3:
result.append(expr(x, y, z))

Now back to our initial example. Let's reformat it a bit

result = [
c.lower() + x % 3 * c  # that's expr(x, c)
for x in range(10) 
if x not in [7,8] 
for c in "aBcDE" 
if c.isupper() 
if c != "B"
]

That looks quite similar to

result = []
for x in range(10) :
if x not in [7,8]:
for c in "aBcDE":
if c.isupper():
if c != "B":
result.append(c.lower() + x % 3 * c)

Whenever you encounter a list comprehension you don't understand immediately 
you can easily translate it into for-loops and if-statements, either by 
reformatting in an editor or in your mind. Similarly you can convert for-
loops appending to a list into a list comprehension. Can you spell

result_loop = []
for x in range(10):
for y in range(10):
if (x + y) % 2:
result_loop.append((x, y))

as a list-comp? Copy the above twice and apply the reverse reformatting 
trick to the second copy.

result_listcomp = [...] # your code
assert result_loop == result_listcomp, "Something went wrong, try again"
print("success")

If you run the script it should print 'success'.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Comprehension Syntax

2012-12-23 Thread Alan Gauld

On 23/12/12 07:48, Mario Cacciatore wrote:


I am having a very hard time understanding the list comprehension
syntax. I've followed the docs and could use some guidance from the fine
folks here to supplement my findings. If someone wouldn't mind replying
back with an example or two, with some explanation of each part I'd
appreciate it.



You could try reading the List Comp sub-section of the Functional 
Programming topic in my tutorial.


That has several examples with explanations and equivalent code etc.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List Comprehension Syntax

2012-12-23 Thread Mitya Sirenef

On 12/23/2012 02:48 AM, Mario Cacciatore wrote:

Hey everyone,

>
> I am having a very hard time understanding the list comprehension 
syntax. I've followed the docs and could use some guidance from the fine 
folks here to supplement my findings. If someone wouldn't mind replying 
back with an example or two, with some explanation of each part I'd 
appreciate it.



Hi Mario, here are some examples (using python3 but very similar in py2.7):


L = range(20)

>>> [x for x in L]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]


[x for x in L if  x<=10]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


[(x,x) for x in L]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), 
(9, 9), (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 
16), (17, 17), (18, 18), (19, 19)]



[x*2 for x in L]

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]


[(x, x*3) for x in L if  x<=10]
[(0, 0), (1, 3), (2, 6), (3, 9), (4, 12), (5, 15), (6, 18), (7, 21), (8, 
24), (9, 27), (10, 30)]



def strmul(x): return  str(x), x*2

...


[strmul(x) for x in L  if x<=10]
[('0', 0), ('1', 2), ('2', 4), ('3', 6), ('4', 8), ('5', 10), ('6', 12), 
('7', 14), ('8', 16), ('9', 18), ('10', 20)]




Hope this helps!


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor