Re: [Tutor] Dict of Dict with lists

2018-04-26 Thread Mats Wichmann
On 04/25/2018 12:46 PM, Kai Bojens wrote:
> On 25/04/2018 –– 18:35:30PM +0100, Alan Gauld via Tutor wrote:
>>> ...
>>> for line in logfile:
>>> result = pattern.search(line)
>  
>> Doesn't this overwrite your data structure?
>> I would strongly advise using another name.
> 
> You are of course right. I accidentally shortened this name as I was trying to
> fit my code into 80 characters width of this mail. That was sloppy ;) 
>  
>> However personally I'd use a class to define tyour data structure and
>> just have a top leveldictionary holding instances of the class.
> 
> You are right (again). I haven't thougt of using classes, but that's exactly
> what they were invented for. Thanks for pointing that out. 

Opinion follows - there's not a right-or-wrong on this, or, I think,
even a "most Pythonic":

Using a bunch of class instances stuffed into one of the built-in
iterable data structures will certainly work fine for this problem.
When I first started learning how to use the "OOP features", not coming
to Python from an OOP background, this is what I did - essentially, if I
would have coded a struct in C, I wrote a class in Python.  I don't do
that any more if there's not any clear benefit to using classes, and I
don't see one for this problem. As a learning excercise, you can try
writing it both ways and see how it feels to you.

In this case the dividing line might be... is this a building block for
some larger complex system where the clarity you get from the layout -
it's all there in the class definition - is important, or is this more
of a "throwaway" single purpose script.

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


Re: [Tutor] Dict of Dict with lists

2018-04-26 Thread Alan Gauld via Tutor
On 26/04/18 14:48, Mats Wichmann wrote:

>>> However personally I'd use a class to define your data structure and
>>> just have a top level dictionary holding instances of the class.
>>
>> You are right (again). I haven't thougt of using classes, but that's exactly
>> what they were invented for. Thanks for pointing that out. 
> 
> Opinion follows - there's not a right-or-wrong on this, or, I think,
> even a "most Pythonic":

That's very true, its about personal preference mostly.

> that any more if there's not any clear benefit to using classes, and I
> don't see one for this problem. 

The advantage of using classes as defined data structures is reliability.

Instead of using strings as names of keys in a dict we get the
advantages of properly defined python attribute names so if
we spell it wrong we get a name error,  and we don't need to
surround everything with ['']. It's not infallible of course
because Python allows us to create attributes on instances at
runtime so spelling errors in assignments still result in
invalid fields being created. But at least we get a measure
of extra checking done by the interpreter and save some typing.

OTOH its definitely not good OOP, ther are no methods and we
are just using the class as a record. (A named tuple might
actually be a better option on reflection.)

> In this case the dividing line might be... is this a building block for
> some larger complex system where the clarity you get from the layout -
> it's all there in the class definition - is important, or is this more
> of a "throwaway" single purpose script.

I'd also add the question of whether there might in fact be some
behaviour that these login records might acquire within the
system. For example formatted printing, comparisons between
records etc etc.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
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] Dict of Dict with lists

2018-04-26 Thread Neil Cerutti
On 2018-04-26, Alan Gauld via Tutor  wrote:
> OTOH its definitely not good OOP, ther are no methods and we
> are just using the class as a record. (A named tuple might
> actually be a better option on reflection.)

namedtuple is great in lots of cases, but sometimes it transpires
I wish to make alterations in the data while I'm working.
Rebuilding tuples on the fly is possible, but painful. With a
class instance it is easy to use __slots__ to help prevent the
accidental creation of new attributes.

>> In this case the dividing line might be... is this a building
>> block for some larger complex system where the clarity you get
>> from the layout - it's all there in the class definition - is
>> important, or is this more of a "throwaway" single purpose
>> script.
>
> I'd also add the question of whether there might in fact be
> some behaviour that these login records might acquire within
> the system. For example formatted printing, comparisons between
> records etc etc.

Excellent point! At the very least, such classes usually gain the
ability to build themselves with an __init__ method, which can be
a convenient place to put any logic they require.

-- 
Neil Cerutti

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


[Tutor] Need help with FileNotFoundError

2018-04-26 Thread Jim
Been working my way through an online Python course. Up until now I have 
had no problems writing and running the programs using Python 3.6 in a 
virtual environment and then pasting them into the courses editor.


When I run this program on my system I get the following error.


# file_io.py

def copy(file, new_file):
with open(file) as data:
text = data.read()


with open(new_file, 'w') as new_text:
new_text.write(text)

copy('~/Documents/Courses/ModernBootcamp/story.txt', 
'~/Documents/Courses/ModernBootcamp/story_copy.txt')



(env36) jfb@jims-mint18 ~ $ python 
'/home/jfb/Documents/Courses/ModernBootcamp/file_io.py'

Traceback (most recent call last):
  File "/home/jfb/Documents/Courses/ModernBootcamp/file_io.py", line 
11, in 
copy('~/Documents/Courses/ModernBootcamp/story.txt', 
'~/Documents/Courses/ModernBootcamp/story_copy.txt')
  File "/home/jfb/Documents/Courses/ModernBootcamp/file_io.py", line 4, 
in copy

with open(file) as data:
FileNotFoundError: [Errno 2] No such file or directory: 
'~/Documents/Courses/ModernBootcamp/story.txt'


The file is there.

jfb@jims-mint18 ~/Documents/Courses/ModernBootcamp $ ls
adding_to_lists.py  errors.py functionsII.pyhelpers.py 
modules.py   stop_copying.py
animals exercise.py   functions.py  iteration.py 
oop.py   story.txt
decorators.py   file_io.pygenerators.py list_comps.py 
__pycache__  unlucky_numbers.py
dictionarys.py  FirstProgram  guessing_game.py  list_methods.py 
smiley_faces.py  while_loop.py


I must be doing something wrong path-wise, but I can't seem to figure it 
out. Any help appreciated.


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


Re: [Tutor] Need help with FileNotFoundError

2018-04-26 Thread Danny Yoo
> copy('~/Documents/Courses/ModernBootcamp/story.txt',
> '~/Documents/Courses/ModernBootcamp/story_copy.txt')


Hi Jim,

You may need to use os.path.expanduser, as "tilde expansion" isn't
something that's done automatically.

This is referenced in the docs when they say: "Unlike a unix shell, Python
does not do any automatic path expansions. Functions such as expanduser()
and expandvars() can be invoked explicitly when an application desires
shell-like path expansion. (See also the glob module.)"

 https://docs.python.org/3/library/os.path.html#module-os.path

Try adding a call to os.path.expanduser()
https://docs.python.org/3/library/os.path.html#os.path.expanduser on that
tilde-prefixed path string.

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


Re: [Tutor] Need help with FileNotFoundError

2018-04-26 Thread Jim

On 04/26/2018 03:27 PM, Danny Yoo wrote:

copy('~/Documents/Courses/ModernBootcamp/story.txt',
'~/Documents/Courses/ModernBootcamp/story_copy.txt')



Hi Jim,

You may need to use os.path.expanduser, as "tilde expansion" isn't
something that's done automatically.

This is referenced in the docs when they say: "Unlike a unix shell, Python
does not do any automatic path expansions. Functions such as expanduser()
and expandvars() can be invoked explicitly when an application desires
shell-like path expansion. (See also the glob module.)"

  https://docs.python.org/3/library/os.path.html#module-os.path

Try adding a call to os.path.expanduser()
https://docs.python.org/3/library/os.path.html#os.path.expanduser on that
tilde-prefixed path string.

Hope this helps!


Danny,

Thanks for pointing me in the right direction. I had tried replacing the 
~ with home/jfb/... now I realize it should have been /home/jfb/... 
Working now.


Thanks,  Jim


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


[Tutor] Sam Kahraman Campus Middle School Advice

2018-04-26 Thread Kahraman, Sam K.
Hello,

 I am a 8th grade student at Campus Middle School. We have a project on coding 
and I need a expert to interview. Currently I'm learning Python and thought I 
should ask someone who works their to Interview. To contact me and ask any 
questions my email is skahra...@cherrycreekschools.org.

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


Re: [Tutor] Sam Kahraman Campus Middle School Advice

2018-04-26 Thread Zachary Ware
Hi Sam,

On Thu, Apr 26, 2018 at 3:00 PM, Kahraman, Sam K.
 wrote:
> Hello,
>
>  I am a 8th grade student at Campus Middle School. We have a project on 
> coding and I need a expert to interview. Currently I'm learning Python and 
> thought I should ask someone who works their to Interview. To contact me and 
> ask any questions my email is skahra...@cherrycreekschools.org.

Can you give some more details on what exactly you're looking for?
This is a fairly unusual request for this mailing list, but I'm sure
we can at least point you in the right direction.  It might be easiest
to just ask your questions here, and you're likely to get more answers
than you bargained for :)

Regards,

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