Basic Python 101 Q ... I think ¿

This is the place for queries that don't fit in any of the other categories.

Basic Python 101 Q ... I think ¿

Postby stubzpub » Mon Jan 06, 2014 7:36 am

How do I access multiple modules, from multiple directories and tie them all together ?

/home/stubz/myShit/Project/2D/calc2D.py
/home/stubz/myShit/Project/3D/calc3D.py
/home/stubz/myShit/Project/Files/file.py
/home/stubz/myShit/Project/Data/file.txt

I'm trying to do calculations and use the results in other calculations and save them as records along the way ...

I'm just not finding a simple, 1,2,3 explanation anywhere ...

I'm also not understanding #!/usr/bin ... Where do you use it? Why do you use it ? What do it actually do and/or mean ?
stubzpub
 
Posts: 8
Joined: Mon Jan 06, 2014 5:32 am

Re: Basic Python 101 Q ... I think ¿

Postby stranac » Mon Jan 06, 2014 8:46 am

Here's an explanation of how the structure of a python package should look like:
Python Packages and You

The shebang just tells your shell what program to run your script with if you run it with
Code: Select all
./myscript

If you google shebang, you should get more information.

You don't need it if you run your scripts with
Code: Select all
python myscript
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Basic Python 101 Q ... I think ¿

Postby stubzpub » Wed Jan 08, 2014 12:39 am

Thanx, that's helped, a little bit ...
I recently moved to Linux Petra - Mint 16 Cinnamon and am liking it, by the way ... :)
I've also recently started to figure out Python ...
I do have some programming experience, VB, Java script, HTML, ++, I'm just not clicking on the Python thing yet.
The code seems pretty straight forward, but the inter active thing and tutorials with a coupla lines of code and bang there it is,
well I'm just not really seeing the big picture of wrapping functions and there results to using that data/info on the next function.
Func(val) gives you a result, depending on that ANS and wanting to use another func() with that result
how do you keep track of the ANS ?
Once you start to get several levels deep and the next func() choice is dependent on some prior result,
the command prompt doesn't really seem all that efficient for keeping score ...
And if you're several levels deep and some thing isn't what you want, to back track and revamp seems like a huge pain ...

Any sorta comment or advice ?
stubzpub
 
Posts: 8
Joined: Mon Jan 06, 2014 5:32 am

Re: Basic Python 101 Q ... I think ¿

Postby Kebap » Wed Jan 08, 2014 10:47 am

Not sure what you mean. Usually, you have one main loop, then call the different functions from there, saving their return values, and maybe giving those to the next functions you call. See this example:

Code: Select all
def my_first_function(input_value):
  # do something
  return output_value

def my_other_function(another_value):
  # do something
  return yet_another_value

def main():
  save_this_value = my_first_function(23)
  save_new_value = my_other_function(save_this_value)

main()


Maybe we can help better if you show your actual code.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Basic Python 101 Q ... I think ¿

Postby stubzpub » Sun Jan 12, 2014 8:34 pm

That seems pretty much what I'm after ... A personal library of functions I can call on
I have a math project I'm working on and I can walk my way down a page of calculations
but it's all about choices ... pick 'something', repeat the same calculation 85x and store the data
do some else to those 85 different entries, another choice, repeat and a 'program' seems like a good solution.
A spread sheet works, but it's not working as well as what I want, I don't really need to see all the work, just the answer(s) ...

I can't seem to get from >>> to the directory up and over several levels away where I built a module with functions in it.

In tutorials, >>> type some snippet in and bang, answer ...
From IDLE create some looping code press f5 and make a list, tuple, dictionary of results

I can't seem to get to that loopCode.py file from >>>
I can't seem to get to a module and call various functions that are in it ... like import math

You've got main() in there, but when I read stuff it's got

if __name__ == '__main__':
main()

or #!/usr/bin/python

or PATH="$PATH:/usr/local/bin/python

and none of seems to do anything when I try to follow along ... Ooops, sorry that doesn't live here ...

I figure it's just some thing small I'm missing or typing wrong, but it just ain't coming together ...

If I have several modules, in several directories, how do I gain access to them and their functions ?
How do I tie them together ?

I think it's a 'program' I want, I guess kinda like a game, with choices.

Fire up python, load my little game and chose some thing.
do 85 calculations and chose some thing else and so on...

I'm pretty sure I can program the functions .. I'm pretty such I can get it to ask me what to do next ...

I just can't seem to find the damn code, in a module that's buried in a directory 'over there' from the >>> prompt ...

Does this make any sense ?
I can do on a page of paper ... it should be doable on a computer ...
I just can't seem to get this, what appears to be described as a simple concept, through my head ...
stubzpub
 
Posts: 8
Joined: Mon Jan 06, 2014 5:32 am

Re: Basic Python 101 Q ... I think ¿

Postby stubzpub » Sun Jan 12, 2014 11:45 pm

I think I found the answer I was looking for under tutorials ...

Advanced search -> modules, with tutorials selected

Modules part 1,2,3 by metulburr

seems to give me the direction to finding the answer I am/was looking for ...
stubzpub
 
Posts: 8
Joined: Mon Jan 06, 2014 5:32 am

Re: Basic Python 101 Q ... I think ¿

Postby metulburr » Mon Jan 13, 2014 5:07 am

Depending on how complex the program is, it might be easier to just put all those files in the same directory. Assuming there is some need to have each file split in

/home/stubz/myShit/Project/2D/calc2D.py
/home/stubz/myShit/Project/3D/calc3D.py
/home/stubz/myShit/Project/Files/file.py
/home/stubz/myShit/Project/Data/file.txt

It all depends on the starting program directory. If your starting script is file.py then its bad programming practice to structure your program like that. For example the structure would best be suited to have the main.py program in the directory Project, and in main.py
Code: Select all
from D2 import calc2D
from D3 import calc3D
from Data import filer
import os
f = open(os.path.join('Files', 'files.txt'))

A couple things about it. from 2D import calc2D will not work as variable names cannot start with a number. So that will cause a syntax error because imports are statements.
Code: Select all
metulburr@arch ~/Untitled Folder 2 $ python3
Python 3.3.3 (default, Nov 26 2013, 13:33:18)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from 2D import calc2D
  File "<stdin>", line 1
    from 2D import calc2D
         ^
SyntaxError: invalid syntax
>>> exit()
metulburr@arch ~/Untitled Folder 2 $ python2
Python 2.7.6 (default, Nov 26 2013, 12:52:49)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from 2D import calc2D
  File "<stdin>", line 1
    from 2D import calc2D
         ^
SyntaxError: invalid syntax
>>>

You should not name variables, functions, classes, module names that of the same as python builtin functions. And file() is one of them. You would be overwriting that built in function if it was file and not filer as shown. And you need a __init__.py file in directory 2D, 3D, and Files for python2.x. you would call attributes then as:
Code: Select all
calc2D.Klass()
calc3D.Klass()
filer()


Or you could:
Code: Select all
import D2.calc2D
import D3.calc3D
import Files.file
D2.calc2D.Klass()
D3.calc3D.Klass()
Files.file()

This method would not overwrite the the builtin file() as you must preceed it with Files

Code: Select all
metulburr@arch ~/Untitled Folder 2 $ ls
D2  D3  Data  Files  main.py
metulburr@arch ~/Untitled Folder 2 $ ls D2
__init__.py calc2D.py
metulburr@arch ~/Untitled Folder 2 $ ls D3
__init__.py  calc3D.py
metulburr@arch ~/Untitled Folder 2 $ ls Data/
__init__.py  file.py
metulburr@arch ~/Untitled Folder 2 $ ls Files/
files.txt
metulburr@arch ~/Untitled Folder 2 $ cat main.py

from D2 import calc2D
from D3 import calc3D
from Data import file
import os
f = open(os.path.join('Files', 'files.txt'))
metulburr@arch ~/Untitled Folder 2 $ python2 main.py
metulburr@arch ~/Untitled Folder 2 $ python3 main.py

and because there were no errors, shows that it was successful
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Basic Python 101 Q ... I think ¿

Postby stubzpub » Mon Jan 13, 2014 5:01 pm

Thanx again,

That structure I built was just an example of the way I was thinking, not necessarily the names I'd be using.
I'm kinda new to linux and python, it's starting to sink in, but I seem to have this thing with having a deep tree.
With python I'm having an issue with the >>>, IDLE and running snippets of code, I couldn't get to them ...
This math project is a personal project, to use my own idea(s) with what I trying learn.

Most tutorials I read seem to jump from basic, 'hello world' to complicated, 'towers of Hanoi' or 'roman numerals'
I can read them, they seem to make sense, but when I try, I get lost pretty quick ...

I'm kinda thinking along the lines of a math text, each section has a topic and solutions.
To write a little snippet that solves each of the equations and stores that algorithm is a particular module
and then have access to that function later on.
Much like the math function, import math and then call on a trig function

It's tying all that stuff together where I'm getting lost, maybe I'm trying to go to big right off the hop, but ...
The tutorials build some bit of code, >>> loop and sort a list or Fibonacci, great little bitz, but what are you doing the the result ?
Presumably you wanted this info for some thing, so why did you ask the Q ?
From >>>, what can you do with the answer and hold on to it for tomorrow ...

IDLE seems to let you write longer ideas, acquire data, make a list, sort it, slice it, del some thing, but to what end ?
It gives multiple results, but tomorrow the datas not there ... and well, I'm not really going to do this for a shopping list ...

Class tutorials are just plan confusing and again, simple or complicated, build a school, teachers/students/employees,
but it's a long term thing, end of semester, end of year, is where things are going to start changing, alot,
and all you get is ... and there's your class ... when ~500 people all have things changing and updating
How did you tell the class that ? What happened to the old data ? Where'd the new data go ?
Database, I think is the answer, but that's not mentioned ... some what more complicated I know,
but to build a right now and not see major changes, there a huge gap ...

Anyway, I'm starting to get some where, but I have issues ...
stubzpub
 
Posts: 8
Joined: Mon Jan 06, 2014 5:32 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests