[Tutor] using separate py files for classes

2011-11-02 Thread Chris Hare

I would like to put each of my classes in separate files to make it easier to 
edit them and keep the various files as small as possible for editing purposes. 
 

I have come across a couple of problems:

1.  I have to use import statements like "from file import class" instead of 
"import file"
2.  I have to include all of the import statements for things not found in the 
class file

Is this normal or am I doing something wrong?

Thanks!

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


Re: [Tutor] using separate py files for classes

2011-11-02 Thread Hugo Arts
On Wed, Nov 2, 2011 at 7:29 AM, Chris Hare  wrote:
>
> I would like to put each of my classes in separate files to make it easier
> to edit them and keep the various files as small as possible for editing
> purposes.
> I have come across a couple of problems:
> 1.  I have to use import statements like "from file import class" instead of
> "import file"
> 2.  I have to include all of the import statements for things not found in
> the class file
> Is this normal or am I doing something wrong?
> Thanks!
> Chris Hare

That's exactly how it's supposed to work, actually. One thing though,
you can use "import file" rather than "from file import class." You
then acces the class like "a = file.class()"

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


Re: [Tutor] login window using Tk

2011-11-02 Thread Alan Gauld

On 02/11/11 05:05, Chris Hare wrote:


 def verifyLogin(self):
 farmid = list.get(ACTIVE)
 userid = login_userid.get()
 login_passwd = login_passwd.get()

gets called, but I get the error

Exception in Tkinter callback
 farmid = list.get(ACTIVE)
AttributeError: type object 'list' has no attribute 'get'

When the frame controls were added, list is defined as

list = Listbox(frame)


names defined in functions are local to that function. They can't be 
seen outside of the function. So...
To be able to access the controls you need to add themas instance 
attributes to your class. so you should use


self.list = Listbox(

and

farmid = self.list.get(

What you are seeing is Python looking for something called list in your 
methods namespace and not finding it. It can't see anything global 
either so it picks up the built-in list() type.


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] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread Alan Gauld

On 02/11/11 06:26, spa...@gmail.com wrote:

Shouldn't this be treated as a bug then? As a user I should be allowed
to uninstall the software I want to.


You can, it just breaks stuff that depends on it.
Just as if you uninstalled Java all your Java applications
would break (eg Eclipse, Freemind etc)

The same is true on any OS. If you remove Java (or .Net)
from a Windows box the same kind of bad thing happens.

The systems administrator (you for a single user PC!) is expected to 
know and understand the critical components and their dependencies.

Basically if you didn't explicitly install it, don't remove it.
(And even if you did explicitly install it there may still be 
dependencies from things you installed later!)


--
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] using separate py files for classes

2011-11-02 Thread Alan Gauld

On 02/11/11 06:29, Chris Hare wrote:


I would like to put each of my classes in separate files to make it
easier to edit them and keep the various files as small as possible for
editing purposes.

I have come across a couple of problems:

1. I have to use import statements like "from file import class" instead
of "import file"
2. I have to include all of the import statements for things not found
in the class file

Is this normal or am I doing something wrong?


That's exactly what happens. Its actually a good thing since it makes 
your code more reliable, although it may not seem like it while you are 
developing it! :-)


You might like to bend your rules of one class per file though. Its 
usually more convenient to have groups of related classes in a single file.


Especially if there is a dependency involved since otherwise the using 
class code winds up with lots of module references in  it. And if you 
come to distribute the code you have to always distribute both files not 
one. And its easy to forget the dependency exists over time...


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

2011-11-02 Thread Alex Hall
Hi all,
I have a class which takes a large number of optional arguments for
its __init__. Instead of going through every single one and assigning
it to "self.[name]", is there some quick way to take all the
parameters of the constructor and assign them all to self.[name] in
one step?

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using separate py files for classes

2011-11-02 Thread Chris Hare

A…. thanks Hugo!!

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

On Nov 2, 2011, at 2:14 AM, Hugo Arts wrote:

> On Wed, Nov 2, 2011 at 7:29 AM, Chris Hare  wrote:
>> 
>> I would like to put each of my classes in separate files to make it easier
>> to edit them and keep the various files as small as possible for editing
>> purposes.
>> I have come across a couple of problems:
>> 1.  I have to use import statements like "from file import class" instead of
>> "import file"
>> 2.  I have to include all of the import statements for things not found in
>> the class file
>> Is this normal or am I doing something wrong?
>> Thanks!
>> Chris Hare
> 
> That's exactly how it's supposed to work, actually. One thing though,
> you can use "import file" rather than "from file import class." You
> then acces the class like "a = file.class()"
> 
> Hugo

___
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-02 Thread Peter Otten
Alex Hall wrote:

> I have a class which takes a large number of optional arguments for
> its __init__. Instead of going through every single one and assigning
> it to "self.[name]", is there some quick way to take all the
> parameters of the constructor and assign them all to self.[name] in
> one step?

You could use something like

http://code.activestate.com/recipes/280381-clean-up-__init__-methods-that-
contain-only-attrib/

or

def __init__(self, **kw):
self.__dict__.update(kw)

but I'd recommend against it. I've never used the recipe myself and instead 
try hard to keep argument list sizes managable.


___
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-02 Thread Christian Witts

On 2011/11/02 03:15 PM, Alex Hall wrote:

Hi all,
I have a class which takes a large number of optional arguments for
its __init__. Instead of going through every single one and assigning
it to "self.[name]", is there some quick way to take all the
parameters of the constructor and assign them all to self.[name] in
one step?


class Test(object):
def __init__(self, param1, param2, param2, **kw):
self.__dict__.update(locals())
self.__dict__.update(kw)

I do prefer assignment by hand, just feels nicer especially when looking 
at it in the future.

--

Christian Witts
Python Developer

//
___
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-02 Thread Alex Hall
Thanks to both of you, that will work. I can see the argument for
assigning everything manually, but I have each parameter on its own
line with a comment to explain what it does, and I know I want all
parameters to be class-level for every instantiation, so saying
"self.[varName]=[varName]" twenty times seems like a waste of time and
space.

On 11/2/11, Christian Witts  wrote:
> On 2011/11/02 03:15 PM, Alex Hall wrote:
>> Hi all,
>> I have a class which takes a large number of optional arguments for
>> its __init__. Instead of going through every single one and assigning
>> it to "self.[name]", is there some quick way to take all the
>> parameters of the constructor and assign them all to self.[name] in
>> one step?
>>
> class Test(object):
>  def __init__(self, param1, param2, param2, **kw):
>  self.__dict__.update(locals())
>  self.__dict__.update(kw)
>
> I do prefer assignment by hand, just feels nicer especially when looking
> at it in the future.
> --
>
> Christian Witts
> Python Developer
>
> //
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread Joel Montes de Oca

On 11/02/2011 02:26 AM, spa...@gmail.com wrote:
Shouldn't this be treated as a bug then? As a user I should be allowed 
to uninstall the software I want to.

Or you uninstalled other things by mistake?

On Wed, Nov 2, 2011 at 6:18 AM, Joel Montes de Oca 
mailto:joelmonte...@gmail.com>> wrote:


On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:

Heh, yeah.  It's usually a bad idea to do stuff like that (I
know a guy (Windows) who deleted his OS of his system).

On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:

I just discovered that it is a bad idea to complete
uninstall Python 2.7 on Ubuntu 11.10. If you do, expect a
lot of things not to work, mainly your system. haha

I just reinstalled Python 2.7 and I hope things are not so
bad now when I reboot.

-- 
-Joel M.


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



Yea, It wiped out GNOME and UNITY along with a few other
applications. It wasn't a big deal tho, I just reinstalled
ubuntu-desktop threw apt-get. :)



-- 
-Joel M.

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




--
http://spawgi.wordpress.com
We can do it and do it better.


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


Spawgi,

Like a few people have mentioned before, this isn't a bug. Unity and 
GNOME rely on Python 2.7 to work, which is fine. When I uninstalled 
Python2.7 GNOME and Unity didn't have a way to run their py files, which 
results in a broken environment. The package manager was smart enough to 
list everything that was going to be uninstalled since they rely on 
Python. So instead of leaving broken systems on my computer, it 
uninstalled all the packages that would had been broken, including Unity 
and GNOME.


I just want to add one thing that hasn't been mentioned yet. The only 
bug was the one called Joel M, which failed to read the uninstall list 
until later when I noticed my environments were being uninstalled.  haha!


Synaptic, the package manager I was using, listed everything that was 
going to be uninstalled but I didn't read it. I didn't realize how many 
system tools used python until later. Live and learn.


It wasn't a big deal. All I had to do was reinstall Python2.7 and 
reboot. Of course, after the reboot, I didn't have an environment to log 
into. So I pressed Ctr + Alt + F1 to get into tty1 and then ran sudo 
apt-get install ubuntu-desktop. That took care of the missing 
environment. Then I restarted with sudo shutdown -r now. I didn't really 
need to do that since I think restarting X was enough but I restarted 
anyhow.


As far as the comments about Ubuntu 11.10 being buggy. I don't think 
it's Ubuntu 11.10 that is buggy, I think it's Unity that is a bit buggy. 
I use Unity and sometimes applications do crash or fail to open when you 
click on their icons... Like I said, I think it's an issue with Unity 
but I can't say for sure since I haven't used another environment since 
I installed Ubuntu 11.10.


--
-Joel M.

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


Re: [Tutor] login window using Tk

2011-11-02 Thread Chris Hare

Just thought I would drop y'all a note and say thank you for your help on this. 
 I have the login code working.

I learned a bunch from you guys.

Thanks!

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

On Nov 2, 2011, at 5:02 AM, Alan Gauld wrote:

> On 02/11/11 05:05, Chris Hare wrote:
> 
>> def verifyLogin(self):
>> farmid = list.get(ACTIVE)
>> userid = login_userid.get()
>> login_passwd = login_passwd.get()
>> 
>> gets called, but I get the error
>> 
>> Exception in Tkinter callback
>> farmid = list.get(ACTIVE)
>> AttributeError: type object 'list' has no attribute 'get'
>> 
>> When the frame controls were added, list is defined as
>> 
>> list = Listbox(frame)
> 
> names defined in functions are local to that function. They can't be seen 
> outside of the function. So...
> To be able to access the controls you need to add themas instance attributes 
> to your class. so you should use
> 
> self.list = Listbox(
> 
> and
> 
> farmid = self.list.get(
> 
> What you are seeing is Python looking for something called list in your 
> methods namespace and not finding it. It can't see anything global either so 
> it picks up the built-in list() type.
> 
> 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] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread spawgi
I use OS X / Windows and I have not noticed any dependency issues. So I was
not aware about the same with respect to Ubuntu. I am glad that I learnt
something from this discussion.

Regards,
Sumod

On Wed, Nov 2, 2011 at 7:40 PM, Joel Montes de Oca
wrote:

> On 11/02/2011 02:26 AM, spa...@gmail.com wrote:
>
>> Shouldn't this be treated as a bug then? As a user I should be allowed to
>> uninstall the software I want to.
>> Or you uninstalled other things by mistake?
>>
>> On Wed, Nov 2, 2011 at 6:18 AM, Joel Montes de Oca <
>> joelmonte...@gmail.com > wrote:
>>
>>On Tue 01 Nov 2011 08:56:41 PM EDT, Max gmail wrote:
>>
>>Heh, yeah.  It's usually a bad idea to do stuff like that (I
>>know a guy (Windows) who deleted his OS of his system).
>>
>>On Nov 1, 2011, at 7:40 PM, Joel Montes de Oca wrote:
>>
>>I just discovered that it is a bad idea to complete
>>uninstall Python 2.7 on Ubuntu 11.10. If you do, expect a
>>lot of things not to work, mainly your system. haha
>>
>>I just reinstalled Python 2.7 and I hope things are not so
>>bad now when I reboot.
>>
>>-- -Joel M.
>>
>>__**_
>>Tutor maillist  - Tutor@python.org 
>>
>>To unsubscribe or change subscription options:
>>
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>>
>>
>>Yea, It wiped out GNOME and UNITY along with a few other
>>applications. It wasn't a big deal tho, I just reinstalled
>>ubuntu-desktop threw apt-get. :)
>>
>>
>>
>>-- -Joel M.
>>__**_
>>Tutor maillist  - Tutor@python.org 
>>
>>To unsubscribe or change subscription options:
>>
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>>
>>
>>
>> --
>> http://spawgi.wordpress.com
>> We can do it and do it better.
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor
>>
>
> Spawgi,
>
> Like a few people have mentioned before, this isn't a bug. Unity and GNOME
> rely on Python 2.7 to work, which is fine. When I uninstalled Python2.7
> GNOME and Unity didn't have a way to run their py files, which results in a
> broken environment. The package manager was smart enough to list everything
> that was going to be uninstalled since they rely on Python. So instead of
> leaving broken systems on my computer, it uninstalled all the packages that
> would had been broken, including Unity and GNOME.
>
> I just want to add one thing that hasn't been mentioned yet. The only bug
> was the one called Joel M, which failed to read the uninstall list until
> later when I noticed my environments were being uninstalled.  haha!
>
> Synaptic, the package manager I was using, listed everything that was
> going to be uninstalled but I didn't read it. I didn't realize how many
> system tools used python until later. Live and learn.
>
> It wasn't a big deal. All I had to do was reinstall Python2.7 and reboot.
> Of course, after the reboot, I didn't have an environment to log into. So I
> pressed Ctr + Alt + F1 to get into tty1 and then ran sudo apt-get install
> ubuntu-desktop. That took care of the missing environment. Then I restarted
> with sudo shutdown -r now. I didn't really need to do that since I think
> restarting X was enough but I restarted anyhow.
>
> As far as the comments about Ubuntu 11.10 being buggy. I don't think it's
> Ubuntu 11.10 that is buggy, I think it's Unity that is a bit buggy. I use
> Unity and sometimes applications do crash or fail to open when you click on
> their icons... Like I said, I think it's an issue with Unity but I can't
> say for sure since I haven't used another environment since I installed
> Ubuntu 11.10.
>
>
> --
> -Joel M.
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
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-02 Thread lina
On Tue, Nov 1, 2011 at 11:56 PM, Dave Angel  wrote:
> On 11/01/2011 11:11 AM, lina wrote:
>>
>> On Tue, Nov 1, 2011 at 10:33 PM, Dave Angel  wrote:
>>>
>>> On 11/01/2011 10:11 AM, lina wrote:

 Hi,

 The following code (luckily) partial achieved what I wanted, but I
 still have few questions:


 #!/usr/bin/python3

 import os.path

 INFILEEXT=".txt"
 OUTFILEEXT=".new"
 DICTIONARYFILE="dictionary.pdb"
 orig_dictionary={}
 new_dictionary={}
 abetaABresidues={}

 def processonefiledata(infilename):
     with open(infilename,"r") as f:
         for line in f:
             parts=line.strip().split()
             orig_dictionary[parts[0]]=parts[1]


 def build_abetadictionary(infilename,olddict):
     with open(infilename,"r") as f:
         for line in f:
             parts=line.strip().split()
             if parts[0] != "85CUR" and (parts[0] not in
 abetaABresidues.keys()):
                 abetaABresidues[parts[0]]=0
         for residues, numbers in abetaABresidues.items():
             if residues in olddict.keys():
                 new_dictionary[residues]=olddict[residues]
             else:
                 new_dictionary[residues]=0
         with open(base+OUTFILEEXT,"w") as f:
             for residues, numbers in new_dictionary.items():
                 print(residues,numbers,file=f)
 ## Q1: How can I sort the results, like the effect of | sort -g

 from something like:
 84ALA 12
 :
 :
 83ILE 28
 :
 :

 to
 :
 83ILE 28
 84ALA 12
 :
>>>
>>> Just use the sort() method of the list object.  In particular, items()
>>> returns an unordered list, so it's ready to be sorted.
>>>
>>>            for residues, numbers in new_dictionary.items().sort():
>>>
>>> That will sort such that residues are in sorted order.
>>
>> Thanks, but still something went wrong here,
>>
>> Traceback (most recent call last):
>>   File "fill-gap.py", line 41, in
>>     build_abetadictionary(DICTIONARYFILE,orig_dictionary)
>>   File "fill-gap.py", line 31, in build_abetadictionary
>>     for residues, numbers in new_dictionary.items().sort():
>> AttributeError: 'dict_items' object has no attribute 'sort'
>>
>
> Peter fixed that one.  Actually my code wasn't even right in Python 2.x, as
> the sort() method sorts in place, and doesn't return a value.  His code will
> work in both 2.x and 3.x, and is thus a much better answer.  I frequently
> confuse the sort method and the sorted function, and judging from this list,
> so do many others.  I'm pretty good at spotting that error if someone else
> makes it ;-)
>
>
>> I have another concerns,
>> is it possible to append the output file content as a sing one,
>> such as a.new is
>> A 1
>> B 3
>>
>> b.new is
>> A 3
>> B 5
>>
>> I wish the final one like:
>>
>> A 1 3
>> B 3 5
>>
>> I will think about it. Thanks,
>
>
> No idea how you came up with a.new or b.new.  Or even what they contain.
>  When you say a.new is "> A 1\n> B 3\n"  I've got to guess you're
> misrepresenting it.
>
> But if you have two dictionaries that use EXACTLY the same keys, and you
> want to print out approximately that way, consider [untested]
>
> def  printme(dict1, dict2):
>    for key in sorted(dict1.keys()):
>         print(key, dict1[key], dict2[key])
>
>
> But notice that it won't print any value which has a key in dict2, but not
> in dict1.  And it'll get an exception if there's a key in dict1 which is not
> in dict2.

Thanks, they shared the same keys. ^_^
>
> So what's your real problem?  There are better ways to accomodate multiple
> sets of related data, and my answer doesn't help if there are 3, or 4, or 42
> dictionaries.
>
> By the way, it's usually better to separate outputting the data from
> inputting.  Factoring code into separate functions makes the code more
> flexible when requirements change.
>
> --
>
> 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-02 Thread lina


Regard the sorted(),

I still have a question,

how to sort something like


>>> results
['1A', '10B', '2C', '3D']
>>> sorted(results)
['10B', '1A', '2C', '3D']

as [ '1A', '2C', '3D','10B']

Thanks,

mainly based on their digital value.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread Prasad, Ramit
>I use OS X / Windows and I have not noticed any dependency issues. So I was 
>not aware about the same with respect to Ubuntu. I am glad that I learnt 
>something from this discussion.

I might be wrong, but Ubuntu itself is not dependent on Python. You can 
probably run a minimalist headless Ubuntu server without Python. Especially 
since the OP seemed able to login to his machine without it. X/Desktop requires 
it.

I am not sure if OS X needs it, but I do know it comes with Python installed.

Joel M: Pretty much the only things that require restarting (from my experience 
with Debian) are kernel changes and hardware changes. You just had to restart X 
/ login manager (gdm).

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
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-02 Thread Dave Angel

On 11/02/2011 11:54 AM, lina wrote:



Regard the sorted(),

I still have a question,

how to sort something like



results

['1A', '10B', '2C', '3D']

sorted(results)

['10B', '1A', '2C', '3D']

as [ '1A', '2C', '3D','10B']

Thanks,

mainly based on their digital value.

Peter answered essentially that question, in his message on this thread, 
yesterday with timestamp 12:14


Essence of the answer is you can supply a key=myfunc  argument to 
sorted().  Then it's up to you what you put in that function.  It sounds 
like you want to convert any leading digits to an int, and return that int.


Once you have that function, you just use it as a keyword argument to 
sorted(), something like:


something =sorted(mylist, key=myfunc)

Why don't you have a go at trying to write such a function?  The exact 
details depend on what you're trying to sort, a list of strings, or a 
list of tuples whose two values are strings.  (or a iterable 
representing one of those).



--

DaveA

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


Re: [Tutor] using separate py files for classes

2011-11-02 Thread Steven D'Aprano

Chris Hare wrote:

I would like to put each of my classes in separate files to make it
easier to edit them and keep the various files as small as possible
for editing purposes.


Perhaps you need a better editor, one with a class browser that lets you 
see the layout of your classes and the relationship between them.


Or perhaps you need smaller classes. I've seen people writing Java in 
Python, and their code ends up anything from 2 to 10 times bigger (and 
consequently slower) than necessary. "One class per file" is very much a 
Java thing.


But without actually seeing your classes, I can't judge.


I have come across a couple of problems:

1.  I have to use import statements like "from file import class"
instead of "import file" 2.  I have to include all of the import
statements for things not found in the class file

Is this normal or am I doing something wrong?


You may be doing something wrong. In Python, it is not normal to force 
the rule "one class per module". You wouldn't say "one method per 
class", would you? In Python, the normal rules are:


* put a group of related methods into a class
* put a group of related classes and functions into a module
* put a group of related modules into a package

Python is designed to work that way, and it is quite unusual to design 
your library or application to be different.




--
Steven
___
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-02 Thread Steven D'Aprano

Alex Hall wrote:

Hi all,
I have a class which takes a large number of optional arguments for
its __init__. Instead of going through every single one and assigning
it to "self.[name]", is there some quick way to take all the
parameters of the constructor and assign them all to self.[name] in
one step?


If your class takes more than, oh, half a dozen parameters, that is 
often Nature's way of telling you the class is badly designed and tries 
to do Too Many Things.


But if you really need to:

def __init__(self, **kwargs):
self.__dict__.update(kwargs)

Add your own error checking :)

Also, the above doesn't work with __slots__.



--
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-02 Thread Alan Gauld

On 02/11/11 16:58, Dave Angel wrote:


sorted(results)

['10B', '1A', '2C', '3D']

as [ '1A', '2C', '3D','10B']




Essence of the answer is you can supply a key=myfunc argument to
sorted(). Then it's up to you what you put in that function. It sounds
like you want to convert any leading digits to an int, and return that int.


Or in your case it might be hex in which case convert the whole string 
using int and specify the base to be 16...


>>> int('1A',16)
26

--
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] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread Joel M.
On Wed, Nov 2, 2011 at 11:39 AM, Prasad, Ramit wrote:

> >I use OS X / Windows and I have not noticed any dependency issues. So I
> was not aware about the same with respect to Ubuntu. I am glad that I
> learnt something from this discussion.
>
> I might be wrong, but Ubuntu itself is not dependent on Python. You can
> probably run a minimalist headless Ubuntu server without Python. Especially
> since the OP seemed able to login to his machine without it. X/Desktop
> requires it.
>
>
Ubuntu is not dependent on Python, but the environments (Unity, GNOME,
etc) will brake if you don't have Python.



> I am not sure if OS X needs it, but I do know it comes with Python
> installed.
>
> Joel M: Pretty much the only things that require restarting (from my
> experience with Debian) are kernel changes and hardware changes. You just
> had to restart X / login manager (gdm).
>
>
Yea I know, but I restarted anyhow.


> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
> --
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> ___
> 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] assign all parameters of __init__ to class variables?

2011-11-02 Thread Steven D'Aprano
On Thu, 3 Nov 2011 12:57:25 am Alex Hall wrote:
> Thanks to both of you, that will work. I can see the argument for
> assigning everything manually, but I have each parameter on its own
> line with a comment to explain what it does, and I know I want all
> parameters to be class-level for every instantiation, so saying
> "self.[varName]=[varName]" twenty times seems like a waste of time
> and space.

Wait, you you want the parameters to be *class level*, that is, shared 
by all instances? But you want them to be set each time you 
instantiate an instance? That's fairly unusual. Normally people want 
each instance to get its own instance-level attributes.

In Python, one way of doing this would be:

class K(object):
a = 1  # class-level shared attribute defined once only
def __init__(self, b):
self.__class__.b = b  # Set the shared class-level attribute


And in action:

>>> ham = K(23)
>>> ham.a, ham.b
(1, 23)
>>> eggs = K(42)
>>> ham.a, ham.b
(1, 42)


Notice that you must assign the attribute on the class object itself, 
not the instance. So if you have a bunch of parameters, rather than 
updating the instance dictionary, you have to update the class 
dictionary. (This makes error checking and parameter validation even 
more important, because you can screw your class up by overwriting 
methods if you pass the wrong parameter name.)

But assuming you like living dangerously, or you trust the caller not 
to be stupid:

class K(object):
a = 1
def __init__(self, **kwargs):
self.__class__.__dict__.update(kwargs)

But having done that, it still won't really work the way you expect 
(or at least the way I *think* you will expect). The problem is that 
the instance still has its own dict, so when the caller assigns to an 
attribute:

instance = K(b=2, c=3)
instance.d = 4

b and c become class-level, as requested, but d is instance-level and 
any other instances will not share that value.

The best way to ensure all instances share the same attributes is with 
the Borg design pattern:

http://code.activestate.com/recipes/66531/

(Also known as Monostate, or the poorly-named "stateless proxy".)

The above recipe is for classic classes, which no longer exist in 
Python 3; for a new-style class version, see the comment by Alex 
Naanou on that page. The other alleged new-style Borgs actually 
implement Singletons.

But if the difference between a Singleton and a Borg matters to you, 
you're probably up to no good. 



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


Re: [Tutor] Python 2.7 on Ubuntu 11.10 - Do not unintall

2011-11-02 Thread Steven D'Aprano
On Thu, 3 Nov 2011 02:02:31 am spa...@gmail.com wrote:
> I use OS X / Windows and I have not noticed any dependency issues.
> So I was not aware about the same with respect to Ubuntu. I am glad
> that I learnt something from this discussion.

In general, you should never uninstall any package you didn't install 
yourself unless you know what the consequences will be.


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


Re: [Tutor] login window using Tk

2011-11-02 Thread Steven D'Aprano
On Wed, 2 Nov 2011 11:54:17 am Alan Gauld wrote:
> On 01/11/11 21:15, Joel Montes de Oca wrote:
> > Question, once the code is compiled to a binary, can someone
> > inject code to cause the hidden window to show, skipping the
> > login altogether?
>
> In general you don't compile Python to a binary, although tools
> exist that give a good approximation to that. But to inject code
> would need access to the source files 

A sufficiently clever byte-code hacker can insert byte-code straight 
into the .pyc file, given write permission to the files -- or an 
exploit that allows writing to a file.

Since people can modify machine code executables (that's how most 
viruses work, and cracked applications), modifying byte-code is 
unlikely to give them any trouble.

Here's a proof-of-concept virus that does exactly that:

http://www.symantec.com/connect/blogs/python-has-venom



-- 
Steven D'Aprano 
___
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-02 Thread Alex Hall
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.

On 11/2/11, Steven D'Aprano  wrote:
> On Thu, 3 Nov 2011 12:57:25 am Alex Hall wrote:
>> Thanks to both of you, that will work. I can see the argument for
>> assigning everything manually, but I have each parameter on its own
>> line with a comment to explain what it does, and I know I want all
>> parameters to be class-level for every instantiation, so saying
>> "self.[varName]=[varName]" twenty times seems like a waste of time
>> and space.
>
> Wait, you you want the parameters to be *class level*, that is, shared
> by all instances? But you want them to be set each time you
> instantiate an instance? That's fairly unusual. Normally people want
> each instance to get its own instance-level attributes.
>
> In Python, one way of doing this would be:
>
> class K(object):
> a = 1  # class-level shared attribute defined once only
> def __init__(self, b):
> self.__class__.b = b  # Set the shared class-level attribute
>
>
> And in action:
>
 ham = K(23)
 ham.a, ham.b
> (1, 23)
 eggs = K(42)
 ham.a, ham.b
> (1, 42)
>
>
> Notice that you must assign the attribute on the class object itself,
> not the instance. So if you have a bunch of parameters, rather than
> updating the instance dictionary, you have to update the class
> dictionary. (This makes error checking and parameter validation even
> more important, because you can screw your class up by overwriting
> methods if you pass the wrong parameter name.)
>
> But assuming you like living dangerously, or you trust the caller not
> to be stupid:
>
> class K(object):
> a = 1
> def __init__(self, **kwargs):
> self.__class__.__dict__.update(kwargs)
>
> But having done that, it still won't really work the way you expect
> (or at least the way I *think* you will expect). The problem is that
> the instance still has its own dict, so when the caller assigns to an
> attribute:
>
> instance = K(b=2, c=3)
> instance.d = 4
>
> b and c become class-level, as requested, but d is instance-level and
> any other instances will not share that value.
>
> The best way to ensure all instances share the same attributes is with
> the Borg design pattern:
>
> http://code.activestate.com/recipes/66531/
>
> (Also known as Monostate, or the poorly-named "stateless proxy".)
>
> The above recipe is for classic classes, which no longer exist in
> Python 3; for a new-style class version, see the comment by Alex
> Naanou on that page. The other alleged new-style Borgs actually
> implement Singletons.
>
> But if the difference between a Singleton and a Borg matters to you,
> you're probably up to no good. 
>
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] binding problem

2011-11-02 Thread Chris Hare

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())
self.list.bind("", 
self.login_userid.focus_set())
self.list.bind("", self.login_userid.focus_set())
self.list.bind("", self.login_userid.focus_set())

If I can get this working, I should be able to get the other bindings to work.
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] frame destroy problem

2011-11-02 Thread Chris Hare

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

2011-11-02 Thread Mike Nickey
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?
I read on the Android Dev site that MonkeyRunner can assist but does
anyone else have any experience on how well this works or if Python is
only really supported through MonkeyRunner?

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.
Thanks in advance.

-- 
~MEN
___
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-02 Thread Dipo Elegbede
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