Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, August 24, 2018

Nexus 3.10 as Python pypi proxy

sudo yum -y update
sudo yum -y install python-pip
pip --help
pip -V
mkdir ~/.pip
cd .pip/
vi pip.conf

enter this:

[global]
index = http://localhost:8181/repository/pypi/
index-url = http://localhost:8181/repository/pypi/simple

Create a nexus Pypi proxy repo "pypi" pointing to https://pypi.org/ . Leave all the defaults as they are.

pip install lino-amici

this should populate your nexus:

Looking in indexes: http://localhost:8181/repository/pypi/simple
Collecting lino-amici
  Downloading http://localhost:8181/repository/pypi/packages/eb/c3/7bdd189fd446effe7ea61d2136dfaec478ec7c5b84f694c6b6e43656f319/lino-amici-18.8.0.tar.gz
Collecting lino-xl (from lino-amici)
  Downloading http://localhost:8181/repository/pypi/packages/02/34/6ab429aac3f80aa64fd00c8f3dd76a9ca9ecf25d7e03f1e48981c7031542/lino-xl-18.4.0.tar.gz (2.3MB)
    100% |████████████████████████████████| 2.3MB 92.2MB/s 
Collecting vobject (from lino-amici)
  Downloading http://localhost:8181/repository/pypi/packages/da/ce/27c48c0e39cc69ffe7f6e3751734f6073539bf18a0cfe564e973a3709a52/vobject-0.9.6.1.tar.gz (58kB)
    100% |████████████████████████████████| 61kB 19.8MB/s 
Collecting lino (from lino-xl->lino-amici)
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPConnectionPool(host='localhost', port=8181): Read timed out. (read timeout=15)",)': /repository/pypi/packages/68/9e/39e5e96b9b8273f0f29943cddd51873ec670657ccf9918727d97ca6628e2/lino-18.8.0.tar.gz
  Downloading http://localhost:8181/repository/pypi/packages/68/9e/39e5e96b9b8273f0f29943cddd51873ec670657ccf9918727d97ca6628e2/lino-18.8.0.tar.gz (10.6MB)
    100% |████████████████████████████████| 10.6MB 89.8MB/s 
Collecting odfpy (from lino-xl->lino-amici)
  Downloading http://localhost:8181/repository/pypi/packages/01/0f/c9971c99d0d06024a1652f467427ff3f1a1136237e5740da715c5b208a48/odfpy-1.3.6.tar.gz (691kB)
    100% |████████████████████████████████| 696kB 42.6MB/s 
Collecting bleach (from lino-xl->lino-amici)
  Downloading http://localhost:8181/repository/pypi/packages/94/aa/0f7ce53f8688bb9f80c0cffacc3964ddfe08321c509c0bfe5062848951f9/bleach-2.1.4-py2.py3-none-any.whl
Collecting weasyprint (from lino-xl->lino-amici)
  Downloading http://localhost:8181/repository/pypi/packages/7e/4c/cf2ec7abf7f84a2d1325d01dcac1d4bcb77f41117101fe564eb76952c65f/WeasyPrint-0.42.3.tar.gz (399kB)
    100% |████████████████████████████████| 409kB 37.7MB/s 
    Complete output from command python setup.py egg_info:
    error in WeasyPrint setup command: Invalid environment marker: python_version < "3.0"
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-sPdLGr/weasyprint/



No worries about the message about the python version.... I have 2.7.... not willing to upgrade for now...






Wednesday, November 22, 2017

Bottle getting started

Looking for an alternative to Django...


Apparently webpy is basically dead. Bottle seems to be alive, and ported to Python 3

http://bottlepy.org/docs/dev/index.html

sudo pip3.6 install bottle

python

paste this code:

from bottle import route, run, template

@route('/hello/')
def index(name):
    return template('Hello {{name}}!', name=name)

run(host='localhost', port=8080)


Enter in the browser:

http://127.0.0.1:8080/hello/world


It can't be simpler! Compare it to the same code in Java...



Django getting started



sudo pip3.6 install Django==1.11.7


follow the instructions https://docs.djangoproject.com/en/1.11/intro/tutorial01/

cd ~
vi .bash_profile
insert this line:
alias python=python3.6
source .bash_profile

django-admin startproject mysite
cd mysite/
python manage.py runserver


Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

November 22, 2017 - 19:02:58
Django version 1.11.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.




in your browser enter http://127.0.0.1:8000/






CentOS 7 install Python 3

CentOS 7 comes with some old version of Python 2 - which is going EOL in a couple of years.

Also, Django recommends Python 3 https://www.djangoproject.com/download/

#this will probably install a new version of docker, and you will likely lose all your containers !!!
sudo yum -y update
#better to reboot now

sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm

sudo yum -y install python36u

#check if installed
python3.6 -V

#beware! old python is still installed, if you run "python" you get the 2 version

https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-centos-7


https://www.djangoproject.com/download/

sudo yum -y install python36u-pip





Thursday, November 16, 2017

set -euf -o pipefail

https://sipb.mit.edu/doc/safe-shell/

nice to read about shell scripting. DON'T USE SHELL, USE PYTHON INSTEAD.

As mentioned in the article, you can use https://docs.python.org/2/library/subprocess.html or also PLUMBUM "Never write shell scripts again"

https://google.github.io/styleguide/shell.xml "Shell should only be used for small utilities or simple wrapper scripts. "



Tuesday, March 31, 2015

Python: tracing value of arguments in functions

If you are lazy (I am) and don't want to write print statements to trace the value of arguments, you can use this trick:
http://stackoverflow.com/questions/6061744/how-to-get-value-of-arguments-in-functions-at-stack

to print the argument values in case of an exception (which is most of the time the only case where you want the values to be traced...)

import inspect
def fn(x):
    try:
        print(1/0)
    except:
        frames = inspect.trace()
        argvalues = inspect.getargvalues(frames[0][0])
        print("Argvalues: ", inspect.formatargvalues(*argvalues))
        raise

fn(12)


('Argvalues: ', '(x=12)')



It works like magic!

I love Python.... if only it was Java, I would love it even more... let's say if Java was more Pythonic it would be a perfect world... well also if USA stopped invading and destroying countries it would not hurt...



Friday, March 27, 2015

Learning Python

I have been using Python for many years, mainly associated with WLST.

As most WLST programmers, I know only the BASIC of the language.... I don't go beyond some essential OO use (yes, I like OO and I hate unstructured procedural programming).

So now I have decided to go through some training and serious (hahaha) study.

This Learn Python interactive tutorial site is just great to get started.

You can read the 4th edition of the great (>1000 pages) Mark Lutz "Learning Python" book for free here - it's definitely very authoritative, and covers both Python 2 and 3.



Honestly I found this book too verbose, not very focused on concepts and giving too many details... still I guess it's a sort of reference training material. But most of the time I find that the answers on stackoverflow are much clearer than this book. Take this post for example , it's a lot clearer than the book...

After some gooogling I found out that http://effbot.org is by far the best educational site for Python.

Oh jah cool, "import this" works also in WLST.... nice...



Tuesday, September 2, 2014

Python: run multiple processes in parallel and wait for completion

This code is quite minimalistic but it works really well. It employs a very old API which is compatible with Pythong 2.1 (WLST very old version of Python....)

from sets import Set

processes = Set()

for command in commandlistlist:
    print "running", command
    processes.add(os.popen(command)) 

#wait until all processes have completed
for proc in processes:
    proc.read()






Thursday, February 27, 2014

Booleans in Python

I firmly believe that a boolean value should be represented by 0 and 1 - yesterday I have wasted 1 hour because in Ruby you say "True" but in YAML it's "true" (morons the Puppet founding fathers who chose Ruby).
How does it work in Python?
value='1'
booleanValue = (value == '1')
print booleanValue
1

value='0'
booleanValue = (value == '1')
print booleanValue
0

BUT:
value=1
if value:
   print "bla"
bla

value=0
if value:
   print "bla"
bla

So there you are, you can make out the rule to convert 0 and 1 into a boolean.

Wednesday, February 26, 2014

YAML parsing in Python with PyYAML

I was looking for a simple way of validating YAML structures, so I did the following:
download http://pyyaml.org/download/pyyaml/PyYAML-3.10.tar.gz
tar xvfz PyYAML-3.10.tar.gz
cd PyYAML-3.10
python setup.py install
python
import yaml
mydomains=yaml.load("""
domains:
  osbpl1do:
    soauser: soa
    optpath: /opt
  osbpl2do:
    soauser: soa2
    optpath: /opt2
  osbpl3do:
    soauser: soa3
    optpath: /opt3
""")
print mydomains
print mydomains['domains']
print mydomains['domains']['osbpl2do']


It works like wonder.
Here more doc.



Friday, December 28, 2012

Python class : getting started

I have never done any OO in Python, but I think I should. OO is good.
Maybe one day will shall say "there was a time where people believed in OO, hahaha what a bunch of losers, now we know that the right thing to do is.... (name your methodology)".

But in the meantime OO is better than Hashtables.

Minimalistic class:
class Pippo:
    name = "hello"


Unlike in Java, this doesn't work:
class Pippo:


there MUST be something inside a class.

Now you can do:
a = Pippo()

this is valid because Pippo has been defined.... if you do
a = Peppo()
you get an error because Peppo class is not defined.

If you do:
a = Pippo()
print a.name
hello

as expected, name was initialized and it's a Object variable, not a Class variable (I mean, it's not static):

a = Pippo()
b = Pippo()
a.name ="bla"
b.name = "mumble"

print a.name
bla
print b.name
mumble

Now let's do:
a.name = "ciao"
a.surname = "bello"
print a


print a.name
ciao

print a.surname
bello

so it's not necessary to declare a member of a class in order to use it. I don't like it, very error prone. I am VERY much in favor of VERY strict syntax.


So it's a MUCH better approach to use mainly Constructors to assign values to attributes:



class Pippo:
    def __init__(self, name, surname):
        self.name = name
        self.surname = surname


a = Pippo("alfa", "beta")

print a.name
alfa
print a.surname
beta


now you can define your classes in a module myclasses.py and then do:

from myclasses import Pippo




Sunday, October 17, 2010

Python!

As a Python IDE I am using Oepe Eclipse 3.6

An excellent tutorial

The entire Python Standard Library

and the language specs

It looks like I will do a lot of WLST/Python... I used to do all my WebLogic monitoring using Java/JMX but it's way too heavy. I love Java but you must be pragmatic.
Python “cheat sheet” - https://www.pcwdld.com/python-cheat-sheet - it’s a quick reference to Python that I think could be useful.
It’s not a cheat sheet in the traditional sense as it’s more about helping someone to get started with Python and then providing some shortcuts to make life easier.