Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39126 > unrolled thread

Re: request for help

Started byleonardo <tampucciolina@libero.it>
First post2013-02-18 21:14 +0100
Last post2013-02-18 13:08 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: request for help leonardo <tampucciolina@libero.it> - 2013-02-18 21:14 +0100
    Re: request for help Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-18 13:08 -0800

#39126 — Re: request for help

Fromleonardo <tampucciolina@libero.it>
Date2013-02-18 21:14 +0100
SubjectRe: request for help
Message-ID<mailman.1969.1361218498.2939.python-list@python.org>
thanks guys and sorry for my incomplete datas, here is the error message:

Traceback (most recent call last):
 File "<pyshell#0>", line 1, in <module>
   import circle
 File "circle.py", line 1
   Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
            ^
SyntaxError: invalid syntax


thanks for any help!






Il giorno 18/feb/2013, alle ore 20:59, Stefan Holdermans <stefan@vectorfabrics.com> ha scritto:

> Leonardi,
> 
>> i saved the above program from python shell into a file as "circle.py" . when i type "import circle" i get  error..
> 
> 
> Next time, please mention what kind of error you're getting.
> 
> Was it an indentation error? Because, as you pasted it, your code would lead to one.
> 
> If I fix the indentation, as in
> 
> import math
> 
> def area(radius):
>   return math.pi * radius**2
> 
> def circumference(radius):
>   return 2 * math.pi * radius
> 
> it works fine for me.
> 
> HTH,
> 
> Stefan
> -- 
> http://mail.python.org/mailman/listinfo/python-list

[toc] | [next] | [standalone]


#39128

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-18 13:08 -0800
Message-ID<7e2605e3-14f3-4ecf-ab27-06bc79b9165b@googlegroups.com>
In reply to#39126
leonardo <tampucciolina <at> libero.it> writes:
> here is the error message:
> [...]

Okay, now we are on the road to solving this problem.

But first we need to take a slight detour and learn about python packaging,
because no matter what the current error is, naming a module "circle" and then
throwing it naked out into the "Python module wilderness" is complete folly; i
can assure you!

============================================================
 What is a Python package?
============================================================

A python package is simply another layer of namespace that protects our symbols
from clashing; in this case: module identifiers.

Even noobs understand that function bodies and class bodies (bka: object
definitions) protect code from outside influences, and that modules protect the
symbols contained in one module from the symbols contained in /other/ modules,
however, we still must protect module identifiers somehow. How do we do this?
Packages to the rescue!

Your "circle.py" module needs to be under the "protective care" of a specialized
package named "geom2d" , which itself should be under the care of a specialized
package named "math", which itself should be under the "global blanket" of a
personal library package named "insertUniqueNameHere"! This is how we protect module symbols whilst simultaneously employing a logical structure in our code.

 Impatient Ivan exclaimed: "So how the heck do i create the package Rick?"

============================================================
Steps to create the entire package hierarchy:
============================================================

Note: All package and modules names should be lowercase!

1a. Create a folder named "mylib" on the Python search path.
1b. Convert the "mylib" folder into a package by adding a file named:
"__init__.py" This will be your personal package for containing your personal
modules.

2a. Inside the "mylib" folder create another folder called "math"
2b. Convert the "mylib\math" folder to a package by adding a file named
"__init__.py".

3a. Inside the "mylib\math" folder create another folder named "geom2d"
3b. Convert the "mylib\math\geom2d" folder to a package by adding a file named
"__init__.py".

4. Inside the "mylib\math\geom2d" folder create a new file named "circlelib.py".
This is where you will place the code for computing circle data. Later you will
probably write something more useful, but for now this module is the best you have.

Now, you'll need to import the circlelib for use and this is how you do it:

## START CODE ##
from mylib.math.geom2d.circlelib import area, circumference
area(blah)
circumference(blah)
## END CODE ##

From now on, if you create any more modules that deal with maths (or a subset of
math: geom) you have a place to store them intelligently. There is quite a bit
more to Python packages but what i describe above is the most fundamental aspect.

============================================================
 Back to your exception
============================================================

Did correcting the indentation fix the problem? If not, what is the next error
you get?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web