[Tutor] Can someone please help me with this?

2013-11-06 Thread Anton Gilb
Hi everyone, I'm a novice student struggling through my first python
course. Currently stuck on how to tackle this exercise. Any input would be
appreciated, and I know you guys can not just give me the answer, I'm just
looking for help to understand how to work through this.

This is the exercise:

It is often adventageous to be able to transfere data between multiple
lists while rearranging their order. For instance, say that list1 =
[1,2,3,4,5,6,7,8,9] and you wish to add the numbers in the index range 4:7
of list1 to another list, list2, in reverse order while simulataneously
removing them from list1. If list2 = [100,200], the result will be list2 =
[100,200,7,6,5]. Write a function named transform that takes as arguments
list1, list2, r1, and r2, that removes items from list1 in the slice r1:r2,
appends them onto list2 in reverse order, and returns the resulting list.
For example, in this case, the function call will be transform(list1,
list2, 4,7).

This is what I've come up with so far, it looks like garbage I know.

list1 = [1,2,3,4,5,6,7,8,9]
list2 = [100,200]
final_list2 = []

def transform(list1, list2, r1, r2):
temp_list = list1[4:7:-1]
final_list2 = list2.append(temp_list)


print(final_list2)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating class / OOP structure

2013-11-06 Thread Mark Lawrence

On 06/11/2013 02:19, Steven D'Aprano wrote:

On Tue, Nov 05, 2013 at 03:55:05PM -0800, Johan Martinez wrote:

I need help in modifying my program. Right now it looks as follows:

[snip code]

Can someone help me in improving my code?


Yes! The first thing to do is get rid of the unnecessary class. This is
not Java where you have to write classes for everything. From the sample
code that you show below, using OOP here accomplishes nothing except
making the code more complicated and less efficient.



Big +1 from me :)

--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: [Tutor] Creating class / OOP structure

2013-11-06 Thread Alan Gauld

On 06/11/13 02:19, Steven D'Aprano wrote:


Yes! The first thing to do is get rid of the unnecessary class. This is
not Java where you have to write classes for everything. From the sample
code that you show below, using OOP here accomplishes nothing except
making the code more complicated and less efficient.


If that's true I agree, but I assumed that since he called it filedict 
he was intending to make it into a filedict and only showed us the 
methods that he was asking about (which is what we encourage people

to do...)


class *needs* only behaviour -- the class doesn't need to store either
self.fname nor self.parsed_file, since they are both only used once.


Its hard to say that if the OP does intend to implement a filedict. If 
the dict is being written back to a file at some point it may need to 
store the original name - or to mangle it and store the new target name...



"Filedict" is neither a file nor a dict -- it doesn't inherit from file,
or behave like a file; it doesn't inherit from dict, or behave like a
dict. It is, in fact, *not* a file/dict at all.


Again, I assumed (possibly wrongly!) that tyhe intent ws to produce a 
filedict (which sounds like it might be based on a shelve?).




What you really have is something with a nasty compound name:

FileParserAndDictProcessor


But if you are right and I'm wrong about the intent then I agree!


When your classes have nasty compound names like this, it is a very
strong clue that the class doesn't actually represent a thing and
probably shouldn't exist



For a very entertaining -- although quite long -- look at this issue,
have a read of this:

http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html


I agreed with about half of this, but strongly disagreed with some of it 
too. Much of it is Java focused and so I understand his frustrations, 
but his comments are a bit to generalised IMHO.


In particular his list of verbs before nouns is completely misguided 
since he uses the imperative case which in English has an implied 
subject - the person carrying out the command. This his examples should 
all be prefixed by a noun to become, for example:


YOU get the garbage bag from under the sink
YOU carry it out to the garage
YOU dump it in the garbage can

And the equivalent OOP constructs become

Person.get(item, location)
Person,put(item, location)
Person.dump(item,receptacle)

In fact in most English sentences the noun does come before the verb.
 is the most common structure.

The bit of his rant thats valid, and often seen in Java, is that the 
verbs get applied to the object of the sentence rather than the

subject. Thus we see

Garbage.get(location)

Which I agree makes no sense.


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

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


Re: [Tutor] Creating class / OOP structure

2013-11-06 Thread Walter Prins
Hi,


On 6 November 2013 08:58, Mark Lawrence  wrote:

> On 06/11/2013 02:19, Steven D'Aprano wrote:
>
>> On Tue, Nov 05, 2013 at 03:55:05PM -0800, Johan Martinez wrote:
>>
>>> I need help in modifying my program. Right now it looks as follows:
>>>
>> [snip code]
>>
>>> Can someone help me in improving my code?
>>>
>>
>> Yes! The first thing to do is get rid of the unnecessary class. This is
>> not Java where you have to write classes for everything. From the sample
>> code that you show below, using OOP here accomplishes nothing except
>> making the code more complicated and less efficient.
>>
>>
> Big +1 from me :)



To belabor the point further, watch this video by Jack Diederich from Pycon
2012 entitled "Stop writing classes":
http://www.youtube.com/watch?v=o9pEzgHorH0

(Don't get me or this video wrong -- classes have their uses but too often
developers try to fit stuff into class structures when the code/problem
really isn't asking for/suits it.)

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


Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread Oscar Benjamin
On 6 November 2013 03:10, Anton Gilb  wrote:
> Hi everyone, I'm a novice student struggling through my first python course.
> Currently stuck on how to tackle this exercise. Any input would be
> appreciated, and I know you guys can not just give me the answer, I'm just
> looking for help to understand how to work through this.

That's fine.

> This is the exercise:
>
> It is often adventageous to be able to transfere data between multiple lists
> while rearranging their order. For instance, say that list1 =
> [1,2,3,4,5,6,7,8,9] and you wish to add the numbers in the index range 4:7
> of list1 to another list, list2, in reverse order while simulataneously
> removing them from list1. If list2 = [100,200], the result will be list2 =
> [100,200,7,6,5]. Write a function named transform that takes as arguments
> list1, list2, r1, and r2, that removes items from list1 in the slice r1:r2,
> appends them onto list2 in reverse order, and returns the resulting list.
> For example, in this case, the function call will be transform(list1, list2,
> 4,7).
>
> This is what I've come up with so far, it looks like garbage I know.
>
> list1 = [1,2,3,4,5,6,7,8,9]
> list2 = [100,200]
> final_list2 = []
>
> def transform(list1, list2, r1, r2):
> temp_list = list1[4:7:-1]
> final_list2 = list2.append(temp_list)
>
>
> print(final_list2)

Which part don't you get? You should say what the code does, why it's
not what you want it to do and whether or not you understand the
behaviour you see.

I'll give one suggestion which is that to concatenate one list onto
the end of another you would use the .extend() method rather than the
.append() method.


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


Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread eryksun
On Tue, Nov 5, 2013 at 10:10 PM, Anton Gilb  wrote:
> Write a function named transform that takes as arguments
> list1, list2, r1, and r2, that removes items from list1 in the slice r1:r2,
> appends them onto list2 in reverse order, and returns the resulting list.
> For example, in this case, the function call will be transform(list1, list2,
> 4,7).
>
> list1 = [1,2,3,4,5,6,7,8,9]
> list2 = [100,200]

For the slice 4 to 7, the reversed slice is 6 down to 3.

list2 += list1[6:3:-1]

>>> list2
[100, 200, 7, 6, 5]

Then del the slice from list1:

del list1[4:7]

>>> list1
[1, 2, 3, 4, 8, 9]

Alternatively you could use pop() and append() in a loop over the
reversed range, though it's not as efficient:

for i in xrange(6, 3, -1):
list2.append(list1.pop(i))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread Alan Gauld

On 06/11/13 03:10, Anton Gilb wrote:


This is what I've come up with so far, it looks like garbage I know.


On the contrary its not too far away.
But we'd rather see a garbage first attempt than no attempt
at all, at least we then know where the problems lie!


list1 = [1,2,3,4,5,6,7,8,9]
list2 = [100,200]
final_list2 = []


You don't need the final_list here


def transform(list1, list2, r1, r2):
 temp_list = list1[4:7:-1]
 final_list2 = list2.append(temp_list)


But you should *return* this value here. This is creating a new local 
variable inside the function, it's not using the global value

you defined above. Also since append() (and extend() for that matter)
do not return a list value final_list2 will always equal None. You
are modifying list2 directly.

Others have commented on the other code inside the function.

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

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


Re: [Tutor] Creating class / OOP structure

2013-11-06 Thread Johan Martinez
On Tue, Nov 5, 2013 at 6:19 PM, Steven D'Aprano  wrote:

> On Tue, Nov 05, 2013 at 03:55:05PM -0800, Johan Martinez wrote:
> > I need help in modifying my program. Right now it looks as follows:
> [snip code]
> > Can someone help me in improving my code?
>
> Yes! The first thing to do is get rid of the unnecessary class. This is
> not Java where you have to write classes for everything. From the sample
> code that you show below, using OOP here accomplishes nothing except
> making the code more complicated and less efficient.
>
> Please don't think I am *against* OOP. But it is a tool, nothing more,
> and you should always use the right tool for the job. And in this case,
> I think the right tool is to create two functions:
>
> def parse(filename):
> with open(filename, 'r') as f:
> ...
> return parsed_file
>
> def process(parsed_file):
> for k in parsed_file:  # Iteration automatically uses keys.
> ...
> return processed_file
>
>
> And then simply call them like this:
>
> processed = process(parse('sales.txt'))
>
> Why is this better?
>
> In the code you show, you don't use the object-oriented structure, so
> why waste time building an object-ordiented structure? Similarly, after
> you have read the file once, the class solution holds onto the contents
> forever. But you don't need it forever, once you have parsed the
> contents they are no longer needed. So why hold on to them longer than
> necessary?
>
> Classes should only be used when you need to encapsulate state plus
> behaviour, in other words, when they represent a *thing*. Classes should
> be nouns: BankAccount, User, DecimalNumber all make good classes. Your
> class *needs* only behaviour -- the class doesn't need to store either
> self.fname nor self.parsed_file, since they are both only used once. In
> effect, they are temporary variables that are given names and made
> long-living:
>
> # no need for this
> filename = "sales.txt"
> parsed_file = parse(filename)
> processed = process(parsed_file)
>
> # now go on to use processed but not filename or parsed_file
> ...
>
> Since those two variables are only used once, there is no need to keep
> them as named variables. Get rid of them!
>
> processed = process(parse('sales.txt'))
>
>
> Now, obviously there are parts of the code you haven't shown us. Perhaps
> you do have good reason for storing parsed_file and filename for later
> use. If so, then my objection to using a class will be reduced, or even
> eliminated.
>
> But even then, there is one last problem with your class: it has a
> dangerously misleading name, which hints that it is not a well-designed
> class.
>
> "Filedict" is neither a file nor a dict -- it doesn't inherit from file,
> or behave like a file; it doesn't inherit from dict, or behave like a
> dict. It is, in fact, *not* a file/dict at all. What you really have is
> something with a nasty compound name:
>
> FileParserAndDictProcessor
>
>
> When your classes have nasty compound names like this, it is a very
> strong clue that the class doesn't actually represent a thing and
> probably shouldn't exist. Don't use classes just as a wrapper around a
> few functions and unrelated variables. (What does the file name have to
> do with the process() method? Why are they stored together?)
>
> For a very entertaining -- although quite long -- look at this issue,
> have a read of this:
>
>
> http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html
>
>
> You don't need to be a Java developer to get value from it.
>
>
> > Should I store parsed_file as an object attribute as follows?
> >
> > class Filedict():
> > def __init__(self, fname):
> > self.fname =fname
> > self.parsed_file = self.parse()
>
> Should you? No. Can you? Yes. But follow the logic -- you could continue
> the process of pushing more code into the __init__ method:
>
> class Filedict():  # Terrible name...
> def __init__(self, fname):
> self.fname =fname
> self.parsed_file = self.parse()
> self.processed = self.process()
>
> f = Filedict("sales.txt")
> processed = f.processed
>
> But now the instance f has no reason to exist, so dump it:
>
> processed = Filedict("sales.txt").processed
>
> which basically means you are using __init__ just as a wrapper to hide
> away a couple of function calls.
>
>
>
> --
> Steven



Thanks for the detailed reply Steven. I am trying to learn class/OOP
structure and selected file parsing and processing as a use case. I wanted
to implement something as follows:

# Create Filedict object - which will return parsed file in dict format
# Where, dict keys are entries in second column and
# dict values are matching lines and number of matching lines

f = Filedict('sales.txt')

The dict representation contains everything I want to know from that file,
however, it's still in raw format. So I need to process it further.

f.process()

The process method splits gives dictionary object in four distinct dict

Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread Alex Kleider

On 2013-11-06 01:52, Oscar Benjamin wrote:



I'll give one suggestion which is that to concatenate one list onto
the end of another you would use the .extend() method rather than the
.append() method.


What would be the advantage/disadvantage of what you suggest vs using 
the plus (+) operand as in

l = l1 + l2
??
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread Dave Angel



Alex Kleider wrote: 

On 2013-11-06 01:52, Oscar Benjamin wrote:
> 
> I'll give one suggestion which is that to concatenate one list onto

> the end of another you would use the .extend() method rather than the
> .append() method.
What would be the advantage/disadvantage of what you suggest vs using 
the plus (+) operand as in

l = l1 + l2
??
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


The plus operator doesn't append to the list; it makes a new one.  Doesn't meet 
spec.

--
Android Usenet Reader
http://android.newsgroupstats.hk

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


Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread Alex Kleider

On 2013-11-06 20:21, Dave Angel wrote:


The plus operator doesn't append to the list; it makes a new one.
Doesn't meet spec.


Right.
It seems then that one could be critical of the assignment in that it is 
requiring a function that has side effects which is something that 
should be discouraged.  To be fair, the name of the function 
('transform') does suggest that it will change something (but does not 
suggest that anything will be returned.)

Comments?

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


Re: [Tutor] Can someone please help me with this?

2013-11-06 Thread Dave Angel
On Wed, 06 Nov 2013 21:42:01 -0800, Alex Kleider  
wrote:
It seems then that one could be critical of the assignment in that 
it is 
requiring a function that has side effects which is something that 
should be discouraged.


Classroom assignments are frequently like that. But without context 
it's hard to know which "rule" should be followed.


The real key is not in critiquing style but realizing exactly what's 
being asked for.


--
DaveA

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