Re: [Tutor] Simple Question On A Method (in subclass)

2011-10-25 Thread Marc Tompkins
On Tue, Oct 25, 2011 at 5:31 AM, Chris Kavanagh  wrote:

>
>
> On 10/25/2011 3:50 AM, Dave Angel wrote:
>
>> On 10/25/2011 12:20 AM, Chris Kavanagh wrote:
>>
>>>
>>>
>>> On 10/24/2011 12:06 AM, Marc Tompkins wrote:
>>>
 On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh >>> 

>>>
>>> My problem was, I wasn't seeing {member} as referring to the class
>>> objects {t} and {s}. Since it was, we now can use member just like any
>>> class object, and combine it with class functions (and class
>>> variables), such as {member.tell}. I had never in my short programming
>>> experience, seen an example like this. So I was confused, obviously, LOL.
>>>
>>>
>> In the context of:
>>
>> t = Teacher('Mrs. Shrividya', 40, 3)
>> s = Student('Swaroop', 22, 75)
>> members = [t, s]
>>
>> for member in members;
>> member.dosomething()
>>
>> member does not refer to t and s at all. It refers to the same object as
>> t and as s, in succession. members is a list of references to objects.
>> Each item in members is bound to a particular object. It is not bound in
>> any way to s or t.
>>
>> For example, suppose we did:
>>
>> members = [t, s]
>> t = 42
>> for member in members:
>> member.dosomething()
>>
>> member still references the object holding Mrs. Shrividya, or Swaroop,
>> in succession, even though t is now (bound to) an integer (object).
>>
>>
>>  I understand. . .Thanks for clearing that up for me Dave.
> So much to learn, so little time! LOL.
> Thanks again to everyone, it's much appreciated.
>

It can be a little hard to wrap your head around how Python handles
variables/objects; in other languages you create a variable and assign a
value to it, while in Python you create an object and assign a name to it -
the name can change while the object remains unchanged.  Here's a very
simplified demo of what Dave is talking about:

>>> t = "This"
>>> s = "That"
>>> members = [t, s]
>>> print members
['This', 'That']
>>> s = "The other"
>>> print members
['This', 'That']


In other words, "members" is NOT a list of "t" and "s" - it's a list of the
objects that "t" and "s" pointed to AT THE MOMENT you created "members".
You can re-assign "t" and "s" to other objects, but the objects that are
members of "members" remain unchanged until you manipulate them directly:

>>> members[0] = "Something else entirely"
>>> print members
['Something else entirely', 'That']

>>> print t, s
This The other

It's extremely logical, but almost entirely backward from the way most other
languages do things.  Possibly it's because Guido is Dutch.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex and parsing through a semi-csv file

2011-10-25 Thread Mina Nozar

Thank you Ramit.  I updated my code since I am running 2.7.1+ on Ubuntu.

Best wishes,
Mina

On 11-10-25 08:02 AM, Prasad, Ramit wrote:

f = open(args.fname, 'r')
lines = f.readlines()
f.close()


If you are using Python 2.6+ you can use a context manager to automatically 
close the file. That way you never have to worry about closing any files!

with open(args.fname, 'r') as f:
 lines = f.readlines()

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] regex and parsing through a semi-csv file

2011-10-25 Thread Prasad, Ramit
>f = open(args.fname, 'r')
>lines = f.readlines()
>f.close()

If you are using Python 2.6+ you can use a context manager to automatically 
close the file. That way you never have to worry about closing any files!

with open(args.fname, 'r') as f:
lines = f.readlines()


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] printing a key not a value

2011-10-25 Thread Joel Goldstick
On Tue, Oct 25, 2011 at 9:45 AM, ADRIAN KELLY wrote:

>  if i have a dictionary called definitions is there a way of printing the
> keys and not the values that go with them?
>
> thanks so much
>
>
>
> Adrian
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> The python.org documentation is a good place to learn about dictionaries.
http://docs.python.org/library/stdtypes.html#mapping-types-dict

As it turns out, you can iterate over the keys of a dictionary:

>>> definitions = {'k1':'k1 value', 'k2': 'k2 value'}
>>> for k in definitions:
...   print k
...
k2
k1



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


Re: [Tutor] printing a key not a value

2011-10-25 Thread Dipo Elegbede
>>> definitions =
{'name':'dipo','food':'spaghetti','age':30,'location':'lagos'}
>>> definitions.keys()
['food', 'age', 'name', 'location']
>>> definitions.values()
['spaghetti', 30, 'dipo', 'lagos']
>>>


You can do this to get what you want.
Hope it helps.

On Tue, Oct 25, 2011 at 2:54 PM,  wrote:

> Sure,
>
> mydict = {'a':1, 'b',2}
> for key in mydict:
>print key
>
> Hope this helps,
> Bodsda
> Sent from my BlackBerry® wireless device
>
> -Original Message-
> From: ADRIAN KELLY 
> Sender: tutor-bounces+bodsda=googlemail@python.org
> Date: Tue, 25 Oct 2011 13:45:50
> To: 
> Subject: [Tutor] printing a key not a value
>
> ___
> 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
>



-- 
Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com
Mobile Banking Solutions | Transaction Processing | Enterprise Application
Development
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing a key not a value

2011-10-25 Thread bodsda
Sure,

mydict = {'a':1, 'b',2}
for key in mydict:
print key

Hope this helps,
Bodsda 
Sent from my BlackBerry® wireless device

-Original Message-
From: ADRIAN KELLY 
Sender: tutor-bounces+bodsda=googlemail@python.org
Date: Tue, 25 Oct 2011 13:45:50 
To: 
Subject: [Tutor] printing a key not a value

___
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] printing a key not a value

2011-10-25 Thread ADRIAN KELLY

if i have a dictionary called definitions is there a way of printing the keys 
and not the values that go with them?
 
thanks so much

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


Re: [Tutor] Simple Question On A Method (in subclass)

2011-10-25 Thread Chris Kavanagh



On 10/25/2011 3:50 AM, Dave Angel wrote:

On 10/25/2011 12:20 AM, Chris Kavanagh wrote:



On 10/24/2011 12:06 AM, Marc Tompkins wrote:

On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh 


My problem was, I wasn't seeing {member} as referring to the class
objects {t} and {s}. Since it was, we now can use member just like any
class object, and combine it with class functions (and class
variables), such as {member.tell}. I had never in my short programming
experience, seen an example like this. So I was confused, obviously, LOL.



In the context of:

t = Teacher('Mrs. Shrividya', 40, 3)
s = Student('Swaroop', 22, 75)
members = [t, s]

for member in members;
member.dosomething()

member does not refer to t and s at all. It refers to the same object as
t and as s, in succession. members is a list of references to objects.
Each item in members is bound to a particular object. It is not bound in
any way to s or t.

For example, suppose we did:

members = [t, s]
t = 42
for member in members:
member.dosomething()

member still references the object holding Mrs. Shrividya, or Swaroop,
in succession, even though t is now (bound to) an integer (object).



I understand. . .Thanks for clearing that up for me Dave.
So much to learn, so little time! LOL.
Thanks again to everyone, it's much appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is wrong with my code?

2011-10-25 Thread apometron

On 10/25/2011 7:34 AM, Dave Angel wrote:
(Once again, please don't top-post.  It makes your responses out of 
order)


On 10/25/2011 04:24 AM, apometron wrote:

I did it very much times, Anssi.

Beyond of run it on Python 2.7 latest build, what do you suggest?

Do install Python 3.2 along the Python 2.7 installation could give me 
any problems?




Why don't you say publicly that you aren't using cmd ?

If your file manager is not running the equivalent of


python  yourprogram.py filename.txt


then everyone here is chasing a wild goose.

Switch to the command line, issue a sequence of commands that cause 
the failure, and paste them in a message here.  Then if it works, but 
doesn't from your file manager, you/we/they can address the 
differences from the working command line.




I found out what it is.

It is the File Commander giving wrong informations to the script.

In Take Command command line it works sweet.

I will show all this to the File Commander author and ask him some way 
to solve this.


It turns out do the thing in command line every time is not best way. I 
need do it by the file manager.


But the file manager was puting stones in the way.

Take Command has a script language also, but I would like do the things 
in Python, if possible.


And this difficulty with File Commander makes use Python a thing less 
easy to do.


Question solved. It was not Take Command the problem and I was sure it 
was not.


Enter in command line to do things is a pain. =( I mean, e-ve-ry ti-me.

But then, good news, all the three scripts works smoothly in the command 
line.


Do you believe drag and drop in the Windows Explorer can be my salvation?

Cool thing to try.

[]s
Apometron
http://about.me/apometron


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


Re: [Tutor] What is wrong with my code?

2011-10-25 Thread Dave Angel

(Once again, please don't top-post.  It makes your responses out of order)

On 10/25/2011 04:24 AM, apometron wrote:

I did it very much times, Anssi.

Beyond of run it on Python 2.7 latest build, what do you suggest?

Do install Python 3.2 along the Python 2.7 installation could give me 
any problems?




Why don't you say publicly that you aren't using cmd ?

If your file manager is not running the equivalent of


python  yourprogram.py filename.txt


then everyone here is chasing a wild goose.

Switch to the command line, issue a sequence of commands that cause the 
failure, and paste them in a message here.  Then if it works, but 
doesn't from your file manager, you/we/they can address the differences 
from the working command line.




--

DaveA

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


Re: [Tutor] What is wrong with my code?

2011-10-25 Thread apometron

I did it very much times, Anssi.

Beyond of run it on Python 2.7 latest build, what do you suggest?

Do install Python 3.2 along the Python 2.7 installation could give me 
any problems?


cheers,
Apometron
http://about.me/apometron

On 10/25/2011 6:11 AM, Anssi Saari wrote:

apometron  writes:


Now it is another
thing, entirely. Rename1.py and Rename2.py works, but why Rename3.py
dont works?

Well, Rename3.py works for me, even in Windows 7. Maybe you should test
it again?


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


Re: [Tutor] string immutability

2011-10-25 Thread Steven D'Aprano
On Tue, Oct 25, 2011 at 12:56:56AM +0100, Alan Gauld wrote:
> On 24/10/11 20:52, Johan Martinez wrote:

> >Finally I figured it out ( __length__() ) thanks to ipython shell env.
> 
> len(x)
> 
> gets converted to x.__length__() by Python.

That's actually __len__ not __length__.


>>> [].__length__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'list' object has no attribute '__length__'
>>> [].__len__



> But personally I just use dir() and help() and Google...

If you use dir() a lot, you might find this useful:

http://code.activestate.com/recipes/54-enhancing-dir-with-globs/


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


Re: [Tutor] Quacks like an object

2011-10-25 Thread Steven D'Aprano
On Mon, Oct 24, 2011 at 07:02:28PM -0400, Christopher King wrote:
> Dear Tutors,
> I am trying to make an object, which will appear exactly like an object
> of my choice. It will should be impossible to tell it is not the object.

Can't be done in Python. The only way to make something absolutely 
indistinguishable from another thing is to actually make them the same thing.

> This is because I am making an object that modifies it methods so that if
> the methods make a change to the object, it will sync those changes to a
> file, but I don't want it to be another object, so foreign functions will
> not mistake for not being the object.

Unfortunately, some functions are excessively finicky about their input. When 
writing a function, the author can:

(1) Just Try It, and if it fails, deal with the failure. Dealing with the 
failure may mean "do nothing, just let the exception happen", or it may mean
catching the exception and do something else.

(2) Check for an interface first, then perform the task, e.g.:

if hasattr(obj, '__getitem__'): x = obj[i]

(3) Check the type using isinstance, which supports subtypes:

if isinstance(obj, list): x = obj[i]

(4) Check for an exact type, which defeats subtyping:

if type(obj) is list: x = obj[i]


There is very little you can do with functions that take the last approach, 
except report it back to the author as a bug.


> For example
> if, I used type on the object, it should return the class of the object it
> is trying to mimic. I have tried everything from modifying the object's get
> set attribute methods, to having making a new object that has get and set
> the same as the object except with a filter for methods, but I had no luck.
> In a nutshell, I want an object that walks like another object, quacks like
> another object, and basically is another object. Is this possible?

No. You have misunderstood duck typing. With duck typing, if your object 
swims like a duck and walks like a duck and quacks like a duck, who cares if 
it's actually a goose? The only way to make it exactly the same as a duck is
if it actually is a duck.

But you can get pretty close. The usual two approaches are:

(A) Delegation. Something like this (untested):

class Wrapper:
def __init__(self, obj):
self['_obj'] = obj
def __getattr__(self, attr):
return getattr(self._obj, attr)
def __delattr__(self, attr):
delattr(self._obj, attr)
def __setattr__(self, attr, value):
setattr(self._obj, attr, value)
def spam(self, arg):
# Save to a file
somefile.write("whatever")
self._obj.spam(arg)  # Call the original.


Delegation works really well for functions that don't use isinstance or type
checking.


(B) Inheritence.

class MySpam(Spam):
# Subclass of Spam that saves to a file.
def spam(self, arg):
somefile.write("whatever")
super(MySpam, self).spam(arg) 



Does this help?


-- 
Steven

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


Re: [Tutor] Simple Question On A Method (in subclass)

2011-10-25 Thread Dave Angel

On 10/25/2011 12:20 AM, Chris Kavanagh wrote:



On 10/24/2011 12:06 AM, Marc Tompkins wrote:

On Sun, Oct 23, 2011 at 8:08 PM, Chris Kavanagh 


My problem was, I wasn't seeing {member} as referring to the class 
objects {t} and {s}. Since it was, we now can use member just like any 
class object, and combine it with class functions (and class 
variables), such as {member.tell}. I had never in my short programming 
experience, seen an example like this. So I was confused, obviously, LOL.




In the context of:

t = Teacher('Mrs. Shrividya', 40, 3)
s = Student('Swaroop', 22, 75)
members = [t, s]

for  member in members;
member.dosomething()

member does not refer to t and s at all.  It refers to the same object 
as t and as s, in succession.  members is a list of references to 
objects.  Each item in members is bound to a particular object.  It is 
not bound in any way to s or t.


For example, suppose we did:

members = [t, s]
t = 42
for member in members:
 member.dosomething()

member still references the object holding Mrs. Shrividya, or Swaroop, 
in succession, even though t is now (bound to) an integer (object).



--

DaveA

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