Hello and welcome to CertForums.co.uk, here we host free active certification forums with links to the best free resources for Microsoft's MCSA MCSE MCDBA Cisco's CCNA CCDA and CCNP, and CompTIA's A+ Network+ i-NET+ and Security+ certifications in the UK. If you wish to post or use other advanced features you will need to register first. Registration is absolutely free and takes only a few minutes to complete so sign up today!

If you have any problems with the registration process or your account login, please contact support

Go Back   CertForums > Computing Support Forums > Programming & Scripting
Home Forums Register Search Today's Posts Mark Forums Read

What are modules and where do they come from?

Post New ThreadReply
 
Thread Tools Display Modes
  #1  
Old 19-Mar-2008, 10:21 PM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,478
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
What are modules and where do they come from?

The tutorial I'm currently working from to learn Python doesn't have a lot of "concept" information which is both good and bad. It's good in that it gets me to writing code fast so I can get my hands dirty and it's bad in that there isn't a lot of explanation to help me understand what things are and why they work.

Chapter 12 is called "Using Modules" (and I finally found out why the "import something" statement is at the top of certain Python programs) but it doesn't really explain what a module is and where it comes from. The "import" implies that it comes from "outside" and into the program but where is its point of origin (I know I'm qualifying myself as a total moron here, but bear with me...no one is born knowing this stuff and I'm operating without the benefit of a degree in Computer Science)?

I've tried looking online, but while I can find a great deal about specific Python modules, I can't find a simple definition. Same with the Python books I have access to. I know other programming languages (Perl, for one) use modules, but can anyone give me a simple, straightforward definition/explanation as to what a module is and where they are magically imported from?

Thanks (once again, chagrined).


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #2  
Old 19-Mar-2008, 10:26 PM
Fergal1982's Avatar
Fergal1982 Fergal1982 is offline CertForums News Posting Member
Their's not to reason why
Posts: 3,030
Points: 4638 Fergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 pointsFergal1982 has over 4000 points
Power: 85
None
Join Date: 04 May 2004
Location: Aberdeen, UK
Age: 25
Certifications: ITIL Foundation
WIP: 70-536,70-294,(A+), Procastination+
This is just a stab in the dark, but it sounds similar to the imports/using features on vb.net/c#. Based on that, I would guess that its importing functions, etc into your script.

In VB.Net, these are dll's (or other blocks of code - usually written by a third party, or by MS). The imports allows you to enter the namespace on the class, so that you can reference the functions.

At its simplest, think about it like this. Say you have a couple of functions that you always use in your scripts. Rather than copying and pasting them into every single script you write, you could write them into a single script, and store it in a specific location. On each script you need it on, you would add the reference to that file, which would allow you to reference those functions. If you ever needed to change it, you would just need to modify one file.

Its like the #include tags in html - you write a portion in a file thats common across a lot of files, and #include it in every file as required.

Like i said, a stab in the dark, but i think im on the right lines.


"Im Nerdy in the extreme and whiter than sour cream"


ObsidianPhoenix - my development blog



 
Reply With Quote
  #3  
Old 19-Mar-2008, 10:33 PM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,478
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Here's one of the sources I've been reviewing. Does this seem to fit what you're describing, Fergal?

http://wiki.python.org/moin/UsefulModules


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #4  
Old 19-Mar-2008, 10:54 PM
dmarsh dmarsh is offline
Lifetime Member
Posts: 1,200
Points: 2781 dmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 pointsdmarsh has over 2500 points
Power: 42
None
Join Date: 24 May 2007
Location: Hampshire
Age: 34
Certifications: One or two...
WIP: OU MST121
Look at this :-

http://www.python.org/doc/2.0.1/tut/node8.html

The important thing to realise is that some programs can have millions of lines of code. Managing complexity and dependencies becomes a real issue. This is where namespaces, modules, libraries, classes and other forms of packaging and ecapsulation come in. They provide but also limit the access to pieces of code from other parts of the system. They make that bit of code usable throughout the system, while preserving code and state in a limited number of places. The DRY principle, (Don't Repeat Yourself), the seperation of concerns, create a good class or library that does one thing well, then leverage and let everone use it. Then you have to manage complexity, creating a layered architecture is one way of doing this. Theres other rules of thumb like avoiding cyclic dependencies between packages where possible.

Theres a lot of rules of thumb, or design principles, at this stage it won't all make sense, just bear in mind creating 'components' is considered good practice as it simplifies the creation of complex systems be they hi-fis, cars or software.

This is a well regarded book on the topic :-

http://www.amazon.com/Large-Scale-So.../dp/0201633620

However unless you're into C++ and working as an architect on a large complex system its probably not for you.

It is a bit dull and drawn out I have to admit that my copy is gathering dust and I never did finish it...



Last edited by dmarsh : 21-Mar-2008 at 09:46 AM.
 
Reply With Quote
  #5  
Old 19-Mar-2008, 11:01 PM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,478
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Thanks for the link to the Modules tutorial. Don't worry about the link to the C++ book. Seems waaaaaaaaaay out of my league.


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #6  
Old 19-Mar-2008, 11:15 PM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,478
Points: 4044 tripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 pointstripwire45 has over 4000 points
Power: 184
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Ok, Mathematix, now I know more about the script you posted in this thread:

http://www.certforums.co.uk/forums/thread23641.html

Code:
import os
<snip>
    
if __name__ == '__main__':
    run()
by reading this: http://www.python.org/doc/2.0.1/tut/node8.html

As I recall from one of your posts in one of my threads, you said you were giving me something to "chew" on.


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #7  
Old 19-Mar-2008, 11:59 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 839
Points: 1149 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 24
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Quote:
Originally Posted by tripwire45 View Post
Ok, Mathematix, now I know more about the script you posted in this thread:

http://www.certforums.co.uk/forums/thread23641.html

Code:
import os
<snip>
    
if __name__ == '__main__':
    run()
by reading this: http://www.python.org/doc/2.0.1/tut/node8.html

As I recall from one of your posts in one of my threads, you said you were giving me something to "chew" on.
What I gave you to chew on was the overall code including the above. The above quoted code allows you to run your script independantly of specifically invoking python.

Anyway, moving onto the importance and relevance of modules. With any programming language there is the notion of having a 'standard way' of performing particular tasks, and the standard way of doing anything is specific to the language in question. The developers of a language determine how tasks like printing to screen, arithmetic, etc. can be most efficiently implemented for the language in the most deterministic way. What do I mean by 'most efficient' and 'deterministic way'? The examples that we have already been working through should appropriately explain the former, in that you presented some code and we utilised the features of Python to squash it down taking fewer instructions to perform the same task. The latter involves ensuring that code specifically has expected results for any input that it expect to receive - that is the notion of 'determinism'. If code does not give one output for any single input, or its input otherwise is dependent on some level of probability, it is said to be 'non-deterministic'. Generally with code, unless the task specifically requires otherwise, all functionality should be deterministic.

Going back to the importance of modules in a programming language. For the reasons described above all language come with standard modules that generally provide the most effecient and determinic tasks that the programmier is likely to need many times over. The programmer is also free to reinvent the wheel if they wish, but their implementation is likely to fall short of the standard provided by the included modules with the language.

Another way to think about it is that Python itself had to be implemented using some other programming language (IIRC, there are C and Java implementations of the language), and these extra modules are merely extensions to the core language.

I'm rambling, and the rest has been explained above. Am stopping now.



Last edited by Mathematix : 20-Mar-2008 at 12:00 AM.
 
Reply With Quote
Post New ThreadReply Spread this thread: Submit this thread to digg Submit this thread to del.icio.us


Go Back   CertForums > Computing Support Forums > Programming & Scripting


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 11:12 PM.

Powered by vBulletin® Version 3.6.11
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
CertForums.co.uk (C) copyright 2003-2007 All Rights Reserved. Content published on CertForums.co.uk requires permission for reprint.
Hosted by Lunarpages