Re: [Tutor] Creating Android Apps w/ Python

2011-11-03 Thread Sander Sweers
On Thu,   3 Nov 2011, 06:12:58 CET, Mike Nickey  wrote:

> I am hoping that there is a Python ADK that will allow creation of
> apps with Python but I haven't seen one and wanted to see what you know is 
> out there.

Linux journal has an article on this [1]. I have no experience in this but it 
looks a good start.
Greets
sander

[1] http://www.linuxjournal.com/article/10940___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] binding problem

2011-11-03 Thread Alan Gauld

On 03/11/11 04:46, Chris Hare wrote:


I have a Listbox defined

self.list = Listbox(self.frame)

What I want to do is when the user either single clicks, double clicks, presses 
tab or return, move the focus to the specified field

 self.list.bind("", self.login_userid.focus_set())


Don't call the function, just pass its name:

self.list.bind("", self.login_userid.focus_set)

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] frame destroy problem

2011-11-03 Thread Chris Hare

Dang it - sure is a typo!
Thanks!

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 12:58 AM, Dipo Elegbede wrote:

> There is nothing called franeButton it should be frameButton. I guess its a 
> typo.
> 
> On 3 Nov 2011 05:52, "Chris Hare"  wrote:
> 
> 
> I have the following code:
> 
>def listUsers(self):
>self.frameBottom = Frame(self.base, bd=0, bg=backColor)
>self.frameBottom.grid(row=1, column=0,sticky=N+E+S+W)
>self.text = Text(self.frameBottom)
>self.text.grid(row=1, column=6, columnspan=5, sticky=E)
>self.text.insert(END, security.listUsers())
>self.btnClose = Button(self.frameBottom, text="Close", 
> command=self.closeFrameBottom,highlightbackground=backColor)
>self.btnClose.grid(row=2, column=4)
> 
>def closeFrameBottom(self):
>self.franeBottom.destroy()
> 
> When the listUsers method is called, everything is displayed correctly.  
> However, when the  btnClose is pressed, I get an error
> 
> Exception in Tkinter callback
> Traceback (most recent call last):
>  File 
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
>  line 1410, in __call__
>return self.func(*args)
>  File "z.py", line 454, in closeFrameBottom
>self.franeBottom.destroy()
> AttributeError: Display instance has no attribute 'franeBottom'
> 
> What have I got wrong? the objective is to use the bottom part opt the window 
> over and over again.
> 
> Chris Hare
> ch...@labr.net
> http://www.labr.net
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] binding problem

2011-11-03 Thread Chris Hare
Thanks for the advice.  When I do that, I get this error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
return self.func(*args)
TypeError: focus_set() takes exactly 1 argument (2 given)

In situations like this where the function isn't one you wrote, how to you 
debug these?

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 3:23 AM, Alan Gauld wrote:

> On 03/11/11 04:46, Chris Hare wrote:
>> 
>> I have a Listbox defined
>> 
>> self.list = Listbox(self.frame)
>> 
>> What I want to do is when the user either single clicks, double clicks, 
>> presses tab or return, move the focus to the specified field
>> 
>> self.list.bind("", self.login_userid.focus_set())
> 
> Don't call the function, just pass its name:
> 
> self.list.bind("", self.login_userid.focus_set)
> 
> 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

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


Re: [Tutor] binding problem

2011-11-03 Thread Dave Angel
(You mistakenly top-posted, so now in order to keep this next message in 
order, I have to delete the out of order history)


On 11/03/2011 09:01 AM, Chris Hare wrote:

Thanks for the advice.  When I do that, I get this error

Exception in Tkinter callback
Traceback (most recent call last):
   File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
 return self.func(*args)
TypeError: focus_set() takes exactly 1 argument (2 given)

In situations like this where the function isn't one you wrote, how to you 
debug these?

Lots of clues in that traceback.  We can deduce that you're probably on 
a non-Windows machine, and that you're using CPython 2.7, and Tkinter.  
Those all would have been good information to include in your initial 
posting.


The other thing it tells you is that your function object focus_set() 
takes one too few arguments.  So, the question is how do you determine 
the types of each of those arguments?


First, write a non-class simple function, that takes two arguments, and 
prints the type() and repr() for each.   Use that function object in the 
self.list.bind function call, and see what it prints when it runs.


Then, once you know the types, you have a clue as to what the real 
function should look like.


We don't know what your self and login_user classes look like, so I was 
trying to eliminate them as contenders for the confusion.


The other thing would be to read the tkinter docs, or find example 
code.  But since I've never used tkinter, I can't give you advice along 
those lines.




--

DaveA

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


Re: [Tutor] binding problem

2011-11-03 Thread Peter Otten
Chris Hare wrote:

> Thanks for the advice.  When I do that, I get this error
> 
> Exception in Tkinter callback
> Traceback (most recent call last):
>   File
>   
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-
tk/Tkinter.py",
>   line 1410, in __call__
> return self.func(*args)
> TypeError: focus_set() takes exactly 1 argument (2 given)
> 
> In situations like this where the function isn't one you wrote, how to you
> debug these?

In this case there is not much to debug, the error message says it all: you 
have an extra argument that the focus_set() method doesn't accept. The 
simplest way to drop that argument is to wrap the method call:

def focus_set(event): # only one arg because it's a function, not a method
self.login_userid.focus_set()

self.list.bind("", focus_set)

This is sometimes written with a lambda function

self.list.bind("", lambda event: self.login_userid.focus_set())

If you wanted to learn more about that extra argument you could temporarily 
replace the callback with something that can give you the necessary 
information, e. g.

def inspect_args(*args):
for arg in args:
 print arg
 print vars(arg)

self.list.bind("", inspect_args)

As a last resort and an idea almost too revolutionary to mention in public, 
you could read some tutorial or documentation...

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


Re: [Tutor] binding problem

2011-11-03 Thread Chris Hare
Thanks Peter.  Actually, I have read a bunch of stuff and looked at example 
code.  The problem in this case is I am using a defined method - focus_set(), 
which is part of Tkinter and isn't part of my code.  since I am using it in the 
manner in which I have seen other examples, I am confused about why it is 
complaining about 2 arguments, when I am not passing any.

Although, I think I know what the problem is now.

Thanks for your help anyway.  

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 9:08 AM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> Thanks for the advice.  When I do that, I get this error
>> 
>> Exception in Tkinter callback
>> Traceback (most recent call last):
>>  File
>> 
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-
> tk/Tkinter.py",
>>  line 1410, in __call__
>>return self.func(*args)
>> TypeError: focus_set() takes exactly 1 argument (2 given)
>> 
>> In situations like this where the function isn't one you wrote, how to you
>> debug these?
> 
> In this case there is not much to debug, the error message says it all: you 
> have an extra argument that the focus_set() method doesn't accept. The 
> simplest way to drop that argument is to wrap the method call:
> 
> def focus_set(event): # only one arg because it's a function, not a method
>self.login_userid.focus_set()
> 
> self.list.bind("", focus_set)
> 
> This is sometimes written with a lambda function
> 
> self.list.bind("", lambda event: self.login_userid.focus_set())
> 
> If you wanted to learn more about that extra argument you could temporarily 
> replace the callback with something that can give you the necessary 
> information, e. g.
> 
> def inspect_args(*args):
>for arg in args:
> print arg
> print vars(arg)
> 
> self.list.bind("", inspect_args)
> 
> As a last resort and an idea almost too revolutionary to mention in public, 
> you could read some tutorial or documentation...
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] binding problem

2011-11-03 Thread Wayne Werner
On Thu, Nov 3, 2011 at 9:41 AM, Chris Hare  wrote:

> Thanks Peter.  Actually, I have read a bunch of stuff and looked at
> example code.  The problem in this case is I am using a defined method -
> focus_set(), which is part of Tkinter and isn't part of my code.  since I
> am using it in the manner in which I have seen other examples, I am
> confused about why it is complaining about 2 arguments, when I am not
> passing any.
>
> Although, I think I know what the problem is now.
>

The problem is that when you use .bind() Tkinter will pass an event into
your function. This event contains useful information such as the x,y
position of your mouse, the key that fired the event, and a few other
items. Of course, since focus_set() belongs to a class it will always pass
'self' as the first parameter.

So when you bind widget.focus_set, when the event loop handles say,
, it finds the function bound (focus_set), and Python passes self
and Tkinter passes the event - two parameters, even though you're passing
none.

As has been touched on, the standard thing to do when you are binding an
event but you don't actually care about the event (like focus_set()), is to
use the lambda, which allows you to ignore the event argument:

self.list.bind("", lambda _: self.login_userid.focus_set())

It's convention to use the _ variable name to tell other programmers that
you don't care about that value, and you're intentionally ignoring whatever
gets passed to that function.

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


Re: [Tutor] binding problem

2011-11-03 Thread Chris Hare
That helps Wayne - and was what I was referring to when I posted that I thought 
I had figured it out.  Thanks for your help.


Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 10:08 AM, Wayne Werner wrote:

> On Thu, Nov 3, 2011 at 9:41 AM, Chris Hare  wrote:
> Thanks Peter.  Actually, I have read a bunch of stuff and looked at example 
> code.  The problem in this case is I am using a defined method - focus_set(), 
> which is part of Tkinter and isn't part of my code.  since I am using it in 
> the manner in which I have seen other examples, I am confused about why it is 
> complaining about 2 arguments, when I am not passing any.
> 
> Although, I think I know what the problem is now.
> 
> The problem is that when you use .bind() Tkinter will pass an event into your 
> function. This event contains useful information such as the x,y position of 
> your mouse, the key that fired the event, and a few other items. Of course, 
> since focus_set() belongs to a class it will always pass 'self' as the first 
> parameter.
> 
> So when you bind widget.focus_set, when the event loop handles say, 
> , it finds the function bound (focus_set), and Python passes self 
> and Tkinter passes the event - two parameters, even though you're passing 
> none.
> 
> As has been touched on, the standard thing to do when you are binding an 
> event but you don't actually care about the event (like focus_set()), is to 
> use the lambda, which allows you to ignore the event argument:
> 
> self.list.bind("", lambda _: self.login_userid.focus_set())
> 
> It's convention to use the _ variable name to tell other programmers that you 
> don't care about that value, and you're intentionally ignoring whatever gets 
> passed to that function.
> 
> HTH,
> Wayne

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


[Tutor] how to separate the digital and the alphabeta

2011-11-03 Thread lina
Hi,

['1AB','57GL', '76LE']

How can I extract 1, 57 , 76 out?

except the one I tried as:

>>> for i in range(len(a)):
print(a[i][:-2])


1
57
76

are there some way to tell the difference between the [0-9] and [A-Z],

Thanks for the help (you will give and you have given).

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


Re: [Tutor] how to separate the digital and the alphabeta

2011-11-03 Thread lina


I have another question, regarding the generator,

def translate_process(dictionary,tobetranslatedfile):
results=[]
with open(tobetranslatedfile,"r") as f:
results=(dictionary[line.split()[2]] for line in f)
print(list(results))
print(len(list(results)))


Here the print(list(results))
did print a long list out,
but why later print(len(list(results)))
print 0 out?

sorry, seems that way is very trivial.

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


Re: [Tutor] how to separate the digital and the alphabeta

2011-11-03 Thread Dave Angel

On 11/03/2011 12:30 PM, lina wrote:

Hi,

['1AB','57GL', '76LE']

How can I extract 1, 57 , 76 out?

except the one I tried as:


for i in range(len(a)):

print(a[i][:-2])


1
57
76

are there some way to tell the difference between the [0-9] and [A-Z],

In the last thread, somebody else gave you a regular expression that 
would separate the digits out.  I avoid regex'es like the plague, for no 
good reason, so I can't help with that approach.


You can tell a particular string is all digits by using the isdigit() 
function.  So some sort of while loop will find it.  Perhaps (untested):


def getkey(item):
"""find the largest prefix of the given string that is all digits.  
return the integer so produced"""

res = "0"
while(res and (res + item[0]).isdigit():
res += item[0]
return int(res, 10)


But your (tiny) sample always has two letters after the digits. Is that 
the rule?  If so, you've got a simple version

def  getkey(item):
 b = item[:-2]
return int(b)



--

DaveA

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


Re: [Tutor] how to separate the digital and the alphabeta

2011-11-03 Thread Dave Angel

On 11/03/2011 12:37 PM, lina wrote:



I have another question, regarding the generator,

def translate_process(dictionary,tobetranslatedfile):
 results=[]
 with open(tobetranslatedfile,"r") as f:
 results=(dictionary[line.split()[2]] for line in f)
 print(list(results))
 print(len(list(results)))


Here the print(list(results))
did print a long list out,
but why later print(len(list(results)))
print 0 out?

sorry, seems that way is very trivial.

Best regards,
_
resuts is a generator, and they can only be iterated through once.  So 
if you need it more than once, convert it to a list first, and save that 
list.  or just use a list comprehension in the first place, (just change 
the parenthese to square brackets)




--

DaveA

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


Re: [Tutor] binding problem

2011-11-03 Thread Alan Gauld

On 03/11/11 13:01, Chris Hare wrote:

Thanks for the advice.  When I do that, I get this error

Exception in Tkinter callback
Traceback (most recent call last):
   File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
 return self.func(*args)
TypeError: focus_set() takes exactly 1 argument (2 given)

In situations like this where the function isn't one you wrote, how to you 
debug these?


Its not a bug in the function, its a bug in your use of bind().

bind() assumes that the function you are binding takes two arguments: 
self and the event. This is different to the function assigned to a 
command parameter which only takes a single (self) argument.


So if you want to use a built-in function that doesn't match what bind 
expects (either more or less arguments) you need to provide a wrapper. 
Usually this will be in the form of a lambda:


Where the function is short of args:

self.list.bind("",
lambda e: the_builtin() )  # drop the event

Where the function needs extra args:

self.list.bind("",
lambda e: aBuiltIn(e, def1, def2) ) )  # supply defaults


However if you are the creator of the method - as seems to be true here 
- you just change the signature of your function/method to take two args 
(and possibly ignore the event).


def myMethod(self, e=None)  # just ignore e if you don't need it
# ...

self.list.bind("", self.myMethod )


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] Creating Android Apps w/ Python

2011-11-03 Thread Steven D'Aprano

Mike Nickey wrote:

I'm currently taking a class on Android Development.
The instructor says that the code needed has to be done through Java.
Isn't there any way to create these same apps with Python?


This is not really the place to be asking this sort of question. This 
list is focused on learning Python the programming language, not about 
the vagaries of using Python on random platforms. If you're lucky, 
somebody here may have done Android development and be able to give a 
useful answer, but you are more likely to get a good response from the 
python-l...@python.org mailing list, also available as comp.lang.python 
on Usenet.


You can also try using a good search engine:

https://duckduckgo.com/html/?q=android%20development%20python

Good luck!



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


Re: [Tutor] Paper Rock Scissors game - User's choice not returned properly

2011-11-03 Thread Steven D'Aprano

Alan Gauld wrote:

Use the list form, even though it does involve a few more keystrokes and 
a lot more screen space. The extra typing to avoid the problems are not 
worth it! :-)


If you're worried about the screen space and/or keystrokes, you can do this:

if direction.upper() in tuple('NSEW'): ...

instead of

if direction.upper() in ('N', 'S', 'E', 'W'): ...


You can also use list, set or frozenset instead of tuple., but I prefer 
tuple because it signals the intention that the result should be read-only.




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


Re: [Tutor] improve the code

2011-11-03 Thread lina
On Wed, Nov 2, 2011 at 12:14 AM, Peter Otten <__pete...@web.de> wrote:
> lina wrote:
>
>>> sorted(new_dictionary.items())
>>
>> Thanks, it works, but there is still a minor question,
>>
>> can I sort based on the general numerical value?
>>
>> namely not:
>> :
>> :
>> 83ILE 1
>> 84ALA 2
>> 8SER 0
>> 9GLY 0
>> :
>> :
>>
>> rather 8 9 ...83 84,
>>
>> Thanks,
>
> You need a custom key function for that one:
>
 import re
 def gnv(s):
> ...     parts = re.split(r"(\d+)", s)
> ...     parts[1::2] = map(int, parts[1::2])
> ...     return parts
> ...
 items = [("83ILE", 1), ("84ALA", 2), ("8SER", 0), ("9GLY", 0)]
 sorted(items, key=lambda pair: (gnv(pair[0]), pair[1]))
> [('8SER', 0), ('9GLY', 0), ('83ILE', 1), ('84ALA', 2)]


Thanks, I can follow the procedure and get the exact results, but
still don't understand this part

parts = re.split(r'"(\d+)",s)

r"(\d+)", sorry,

>>> items
[('83ILE', 1), ('84ALA', 2), ('8SER', 0), ('9GLY', 0)]


>>> parts = re.split(r"(\d+)",items)
Traceback (most recent call last):
  File "", line 1, in 
parts = re.split(r"(\d+)",items)
  File "/usr/lib/python3.2/re.py", line 183, in split
return _compile(pattern, flags).split(string, maxsplit)
TypeError: expected string or buffer

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


Re: [Tutor] improve the code

2011-11-03 Thread lina
On Fri, Nov 4, 2011 at 10:39 AM, lina  wrote:
> On Wed, Nov 2, 2011 at 12:14 AM, Peter Otten <__pete...@web.de> wrote:
>> lina wrote:
>>
 sorted(new_dictionary.items())
>>>
>>> Thanks, it works, but there is still a minor question,
>>>
>>> can I sort based on the general numerical value?
>>>
>>> namely not:
>>> :
>>> :
>>> 83ILE 1
>>> 84ALA 2
>>> 8SER 0
>>> 9GLY 0
>>> :
>>> :
>>>
>>> rather 8 9 ...83 84,
>>>
>>> Thanks,
>>
>> You need a custom key function for that one:
>>
> import re
> def gnv(s):
>> ...     parts = re.split(r"(\d+)", s)
>> ...     parts[1::2] = map(int, parts[1::2])
>> ...     return parts
>> ...
> items = [("83ILE", 1), ("84ALA", 2), ("8SER", 0), ("9GLY", 0)]
> sorted(items, key=lambda pair: (gnv(pair[0]), pair[1]))
>> [('8SER', 0), ('9GLY', 0), ('83ILE', 1), ('84ALA', 2)]
>
>
> Thanks, I can follow the procedure and get the exact results, but
> still don't understand this part
>
> parts = re.split(r'"(\d+)",s)
>
> r"(\d+)", sorry,
>
 items
> [('83ILE', 1), ('84ALA', 2), ('8SER', 0), ('9GLY', 0)]
>
>
 parts = re.split(r"(\d+)",items)
> Traceback (most recent call last):
>  File "", line 1, in 
>    parts = re.split(r"(\d+)",items)
>  File "/usr/lib/python3.2/re.py", line 183, in split
>    return _compile(pattern, flags).split(string, maxsplit)
> TypeError: expected string or buffer

Sorry, now works.

parts = re.split(r"(\d+)", str(items))
>
> Thanks,
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] improve the code

2011-11-03 Thread Dave Angel

On 11/03/2011 10:39 PM, lina wrote:

On Wed, Nov 2, 2011 at 12:14 AM, Peter Otten<__pete...@web.de>  wrote:

lina wrote:



items

[('83ILE', 1), ('84ALA', 2), ('8SER', 0), ('9GLY', 0)]



parts = re.split(r"(\d+)",items)

Traceback (most recent call last):
   File "", line 1, in
 parts = re.split(r"(\d+)",items)
   File "/usr/lib/python3.2/re.py", line 183, in split
 return _compile(pattern, flags).split(string, maxsplit)
TypeError: expected string or buffer

Thanks,
This email of Peter's is what I was referring to as regex.  The re 
module is for processing regular expressions.  You'll have to go to 
someone else for help in it, but I can point out that you can only 
process one string with that line.  You're handing it a whole list.


So something like:

for item in items:
  parts = re.split(.
  print (repr(parts))

might show you what it's doing.  I suspect it'll give you a tuple with 
the numeric part first, and the non-numeric part second.




--

DaveA

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


Re: [Tutor] improve the code

2011-11-03 Thread Peter Camilleri
Not sure if that is what you are after since you are calling
re.split() using items when Peter was using re.split() on a single
item within items

so instead of this
parts = re.split(r"(\d+)", str(items))
try specifying just one item, like this
parts = re.split(r"(\d+)", items[0])

On Fri, Nov 4, 2011 at 1:42 PM, lina  wrote:
> On Fri, Nov 4, 2011 at 10:39 AM, lina  wrote:
>> On Wed, Nov 2, 2011 at 12:14 AM, Peter Otten <__pete...@web.de> wrote:
>>> lina wrote:
>>>
> sorted(new_dictionary.items())

 Thanks, it works, but there is still a minor question,

 can I sort based on the general numerical value?

 namely not:
 :
 :
 83ILE 1
 84ALA 2
 8SER 0
 9GLY 0
 :
 :

 rather 8 9 ...83 84,

 Thanks,
>>>
>>> You need a custom key function for that one:
>>>
>> import re
>> def gnv(s):
>>> ...     parts = re.split(r"(\d+)", s)
>>> ...     parts[1::2] = map(int, parts[1::2])
>>> ...     return parts
>>> ...
>> items = [("83ILE", 1), ("84ALA", 2), ("8SER", 0), ("9GLY", 0)]
>> sorted(items, key=lambda pair: (gnv(pair[0]), pair[1]))
>>> [('8SER', 0), ('9GLY', 0), ('83ILE', 1), ('84ALA', 2)]
>>
>>
>> Thanks, I can follow the procedure and get the exact results, but
>> still don't understand this part
>>
>> parts = re.split(r'"(\d+)",s)
>>
>> r"(\d+)", sorry,
>>
> items
>> [('83ILE', 1), ('84ALA', 2), ('8SER', 0), ('9GLY', 0)]
>>
>>
> parts = re.split(r"(\d+)",items)
>> Traceback (most recent call last):
>>  File "", line 1, in 
>>    parts = re.split(r"(\d+)",items)
>>  File "/usr/lib/python3.2/re.py", line 183, in split
>>    return _compile(pattern, flags).split(string, maxsplit)
>> TypeError: expected string or buffer
>
> Sorry, now works.
>
> parts = re.split(r"(\d+)", str(items))
>>
>> Thanks,
>>>
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Paper Rock Scissors game - User's choice not returned properly

2011-11-03 Thread Alan Gauld

On 04/11/11 01:42, Steven D'Aprano wrote:

Alan Gauld wrote:



If you're worried about the screen space and/or keystrokes, you can do
this:

if direction.upper() in tuple('NSEW'): ...


It's not so much the keystrokes that I don't like but it's the fact that 
I invariably miss out a quote or a comma every time I try to construct a 
long sequence of single character strings. This avoids

that by getting Python to do the work for me.

I like it! The best of both worlds. :-)


Thanks Stephen,

--
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] assign all parameters of __init__ to class variables?

2011-11-03 Thread Steven D'Aprano

Alex Hall wrote:

I'm sorry, I misspoke (well, mistyped anyway). I have a couple
class-level variables, but most of them are set in the __init__ so
that every instance gets a fresh copy of them. Thatnks for the
responses.



Ah I see, or at least I think I see. Possibly we're talking at 
cross-purposes.


When you talk about "class-level variables", to me that means class 
attributes: attributes of a class, like this:


class Spam:
x = "this is at the class level"

as opposed to "instance-level variables", which I would interpret as 
instance attributes:



class Ham:
def __init__(self):
self.x = "this is on the instance"


If you mean something different from this, then we're talking past each 
other.




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