Author Topic: Python: Less is More  (Read 722 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Python: Less is More
« on: September 08, 2015, 11:11:36 pm »
You know, there are several IDEs one could use to write python scripts.  Since I might be leaning strongly in the direction of becoming a Python evangelist, I do not want to discourage you (whoever may be reading this) by leading you to software that may just annoy you.

I would prefer you experienced the raw power at the most basic level since that will give you independence.  Python is cross-platform, and the interpreter is the same in both Unix and Windows.  I'm starting to just call Linux Unix to avoid confusion.

Attached is a text file.  If you download it, immediately open it in emacs or a non-word-processor like Notepad++, and rename it list_factors.py

Again, I have to emphasize that I would like this  particular thread to be focused on something simple yet powerful.

Using our own self-defined functions in a Python interpreter, the same scripts in both Linux(UNIX) and Windows (or even Macintosh, for that matter).

In this thread I will not be mentioning all the IDEs like PyCharm or Eclipse or Python Tools for Visual Studio.  I want this thread to just be about the interpreter and how to use our own scripts or those we want to use from packages "in the PYTHONPATH".

After installing Python 2.7.X, you can just create a directory called py in your home directory.

/home/H/py
or
C:\Users\H\py

Linux and Windows, respectively

From a terminal (like cmd.exe command prompt) change to the directory py where you place list_primes.py

It is a very small file meant to show how to use our own functions, since when first using Python, modules can seem mysterious --- packages and all that, like numpy, math, os, sys, etc
 
Suppose you were to start the Python interpreter (typing the word python at the command prompt in the directory where list_factors.py is.  You can add this directory to a path later if you decide writing and using your own scripts is something that interests you.

You will get the prompt of the Python interpreter.

>>>

This is how to use the little homemade function, as an example.

>>> from list_factors import primeFactors
>>> primeFactors((2222222)

It will output [2, 239, 4649]


If you wanted to use a debugger to step through that code, that is a fun and powerful way to understand what is going on by stepping through the lines of the code.

I think this is a more exciting way to learn rather than struggling to get high-end IDE functioning.

You can skip all that annoyance and get right to the real magic.

Saving the .py file to .txt is just a way to trick this message board.  It will not upload .py files.

peace

oh, and remember one thing:  if you are installing Python in Microsoft Windows, if you are interested in Math, then packages like numpy are essential and not easily set up in Windows.  You can eliminate unnecessary grief by installing the 32-bit Anaconda package (python.exe) which has numpy and many other mathematics packages which will be accessible.
« Last Edit: September 09, 2015, 12:23:25 am by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Share on Facebook Share on Twitter


Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Anaconda and VPython
« Reply #1 on: July 24, 2017, 03:53:18 pm »
After a few days of getting Gentoo-based Funtoo installed (to replace Gentoo-based Sabayon), I decided to use Anaconda to set up my python environments since I really enjoy using "isympy" (SymPy in an IPython session).  I wanted also to use VPython.

Since there are many links to contradictory instructions on how to go about setting this up, and after finding a method that is straight-forward, I figured I would post this for posterity in case anyone lurking here happens to want to explore SymPy and VPython.  This is a smooth way to set it up in Linux or Windows (and probably Mac OSX as well).

First go to https://www.continuum.io/downloads and download Anaconda for Python 3.6 (we can set up Python 2.7 and VPython environments afterward with conda).

On Linux, I ran on command line:  bash Anaconda3-4.4.0-Linux-x86_64.sh

I let it install in /home/hentrich/anaconda3

I also let it add to the PATH in .bashrc

Then I created Python2 environment:

conda create -n py2 python=2.7 anaconda

To be safe, you might even want to create a separate environment for vpython:

conda create -n vpy python=2.7 anaconda

source activate vpy

conda install -c vpython vpython

jupyter notebook

Once this loads the web-browser at localhost:8888/tree,  New | Vpython

from vpython import *
s = sphere()

[hit shift+Enter] : If all is well, you should see a sphere in 3D.  If not, you may not be connected to the "server."   This implies that there is a problem with not connecting to kernel.  There may be signs of error 403 "Couldn't authenticate WebSocket connection".   This happened to me when running in Slackware with Konqueror web browser.  Making Firefox your default web browser may solve this issue.  (System Settings | Default Applications | Web Browser | "In the following browser" | […] find Firefox under Internet)

___________________________________________________________________
What to do with VPython?  [PHYSICS !]:  Check out Matter and Interactions and vpython.org.

Again, this was just for posterity (and for a future version of my so-called "self").

In the meantime, with the installation of Anaconda, even if you never use Vpython, you are sure to be drawn to SymPy.

You can type isympy at the command line and have a computer algebra system at your fingertips.

Note:  For some odd reason, in certain "Microsoft Windows" systems, even after "conda install -c vpython vpython", in jupyter notebook, sphere() is not defined when "from vpython import *"

Sorry for all the jargon-gibberish.

In such a case, I found the following works.

import ivisual
s = ivisual.sphere()

You just have to be a little flexible and exploratory.




« Last Edit: November 10, 2018, 04:59:20 am by {{}} »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4766
  • Life teaches me not to want it.
    • What Now?
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~