Python

From Wiki2
Revision as of 11:37, 14 December 2020 by Tim (talk | contribs)

python

tutorial

2.10.1 Subprograms

One of the most important in mathematics concept is to use functions. Functions in computer languages implement mathematical functions. The executing function produces one or more results, which are dependent by the parameters passed to it.

In general, a function is a structuring element in the programming language which groups a set of statements so they can be called more than once in a program. Programming without functions will need to reuse code by copying it and changing its different context. Using functions enhances the comprehensibility and quality of the program. It also lowers the memory usage, development cost and maintenance of the software.

Different naming is used for Functions in programming languages, e.g. as subroutines, procedures or methods.

Python language defines function by a def statement. The function syntax looks:

def function-name(Parameter list):

 statements, i.e. the function body

Function bodie can contain one or more return statement. It can be situated anywhere in the function body. A return statement ends the function execution and returns the result, i.e. to the caller. If the return statement does not contain expression, the value “None” is returned.

Python

def Fahrenheit(T_in_celsius):

 """ returns the temperature in degrees Fahrenheit """
 return (T_in_celsius * 9 / 5) + 32

for t in (22.6, 25.8, 27.3, 29.8):

 print(t, ": ", fahrenheit(t))

Output:

Python

>>> 22.6 : 72.68 25.8 : 78.44 27.3 : 81.14 29.8 : 85.64 >>>

jupyter on python3

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-jupyter-notebook-to-run-ipython-on-ubuntu-16-04


is installed in omen www/environments/jup

tim@omen:/d/fs/www/environments$ pyvenv jup
tim@omen:/d/fs/www$ cd environments/
tim@omen:/d/fs/www/environments$ . jup/bin/activate
(jup) tim@omen:/d/fs/www/environments$ jupyter notebook

(jup) tim@omen:/d/fs/www/environments/jup/bin$ deactivate

accessing object properties

status ={}
status['pond'] = {'spot': 'center', 'state': 'off', 'tleft': 0, 'nexton': 9}
status['pond']['tleft']= 16

parsing u'5'

basic tutorial

https://thenewcircle.com/static/bookshelf/python_fundamentals_tutorial/advanced_types_containers.html

running app forever - supervisorctl

cascada

port = 8087
/home/pi/mypi/cascada/server/cascada.py

cascada2

port = 8088
/home/pi/mypi/cascada/server/cascada2.py

<markdown>

sudo`supervisorctl` reads configuration from `/etc/supervisor/conf.d` and runs whatever files if finds there.

a typical `conf` ile looks like:

   [program:cascada2]
   command=/usr/bin/python /home/pi/mypi/cascada/server/cascada2.py
   directory=/home/pi/mypi/cascada/server
   autostart=true
   autorestart=true
   startretries=3
   stderr_logfile=/var/log/cascada/cascada2.err.log
   stdout_logfile=/var/log/cascada/cascada2.out.log
   user=root
   environment=SECRET_PASSPHRASE='this is secret',SECRET_TWO='another secret'

you may have to `>supervisor reload` then `ctrl c` the `sudo supervisorctl` to get a new program running forever

</markdown>

pi@raspberrypi ~ $ sudo supervisorctl
cascada                          RUNNING    pid 2273, uptime 7 days, 3:17:36


tail -f /var/log/cascada/cascada.out.log
tail -f /var/log/cascada/cascada.err.log

after changing cascada.py

pi@raspberrypi ~ $ sudo supervisorctl
cascada                          RUNNING    pid 2273, uptime 7 days, 3:17:36
supervisor> stop cascada

exit w ctrl C

sudo lsof -i :8087
sudo kill -9 21118 (kill whatever port lsof returns)
pi@raspberrypi ~ $ sudo supervisorctl
cascada                          STOPPED    Jul 16 04:34 PM
supervisor> start cascada
cascada: started
supervisor>

exit w ctrl C ??

  • nohup python app.py &
  • use screen
  • run supervisord(link) on system startup and control all through it (pythonic way :))

nohup means: do not terminate this process even when the stty is cut off.

& at the end means: run this command as a background task.

forever with supervisord

SSE - Server Side Events

http://flask.pocoo.org/snippets/116/

https://github.com/stevenewey/ssedemo


for node

https://www.npmjs.com/package/simple-sse (has room,haven't tried)

https://tomkersten.com/articles/server-sent-events-with-node/

http://www.futureinsights.com/home/real-time-the-easy-way-with-eventsource-angularjs-and-nodejs.html

SocketIO

http://stackoverflow.com/questions/17641602/how-to-emit-to-room-in-socket-io

flask socketio

https://flask-socketio.readthedocs.org/en/latest/