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

Thursday, August 20, 2020

Conda virtual environment management

To list all env:
conda env list


To create:
conda create --name myenv

To create with a specific version of python
conda create -n myenv python=3.6

To activate:
conda activate myenv

To go back to base env:
conda deactivate

Saturday, February 21, 2015

Python after years

Recently I've been installing python on Windows. I haven't done it for few years now. I was surprised that msi installer by default installs pip with its dependencies. It also comes with a basic IDE. This is really nice. Pip seams to be a standard now. But definitely there are few things that didn't change. A quick look at Django code:

def b85decode(b):
        _b85dec = [None] * 256
        for i, c in enumerate(iterbytes(_b85alphabet)):
            _b85dec[c] = i

        padding = (-len(b)) % 5
        b = b + b'~' * padding
        out = []
        packI = struct.Struct('!I').pack
        for i in range(0, len(b), 5):
            chunk = b[i:i + 5]
            acc = 0
            try:
                for c in iterbytes(chunk):
                    acc = acc * 85 + _b85dec[c]
            except TypeError:
                for j, c in enumerate(iterbytes(chunk)):
                    if _b85dec[c] is None:
                        raise ValueError(
                            'bad base85 character at position %d' % (i + j)
                        )
                raise
            try:
                out.append(packI(acc))
            except struct.error:
                raise ValueError('base85 overflow in hunk starting at byte %d'
                                 % i)

Ech, not much changed since I was looking at python programming style years ago. Its not language fault, it is more about how well people are writing open source code. How much does it take to understand what a method above is doing - the method name is a great help, but to understand implementation takes more than few minutes, and it is far more time than few seconds.

Saturday, September 8, 2012

pylint and pep8

I've spend some time analyzing Django code. Maybe because I return to python with a huge experience in C#, but I find a Django code really messy. Classes, methods are big, many examples of violation of SOLID rules, and so on. I decided to test it against typical code analysis tools for python - pylint and pep8. Below are some results. By the way there is also django-lint - unfortunately not available via pip.
pylint db\models\query.py
...

Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |85     |NC       |NC         |
+-----------+-------+---------+-----------+
|refactor   |26     |NC       |NC         |
+-----------+-------+---------+-----------+
|warning    |98     |NC       |NC         |
+-----------+-------+---------+-----------+
|error      |22     |NC       |NC         |
+-----------+-------+---------+-----------+

...
Global evaluation
-----------------
Your code has been rated at 6.52/10
pep8 db\models\query.py
db\models\query.py:12:5: E128 continuation line under-indented for visual indent
db\models\query.py:28:1: E302 expected 2 blank lines, found 1
db\models\query.py:53:14: E231 missing whitespace after ','
db\models\query.py:54:29: E231 missing whitespace after ','
db\models\query.py:174:17: E127 continuation line over-indented for visual indent
db\models\query.py:269:80: E501 line too long (83 > 79 characters)
db\models\query.py:290:80: E501 line too long (81 > 79 characters)
db\models\query.py:315:50: E225 missing whitespace around operator
db\models\query.py:328:80: E501 line too long (88 > 79 characters)
db\models\query.py:336:17: E128 continuation line under-indented for visual indent
db\models\query.py:366:21: E128 continuation line under-indented for visual indent
db\models\query.py:367:80: E501 line too long (127 > 79 characters)
db\models\query.py:368:17: E128 continuation line under-indented for visual indent
db\models\query.py:408:80: E501 line too long (90 > 79 characters)
db\models\query.py:409:17: E125 continuation line does not distinguish itself from next logical line
db\models\query.py:410:80: E501 line too long (84 > 79 characters)
db\models\query.py:412:80: E501 line too long (87 > 79 characters)
db\models\query.py:414:80: E501 line too long (96 > 79 characters)
db\models\query.py:416:80: E501 line too long (144 > 79 characters)
db\models\query.py:434:17: E127 continuation line over-indented for visual indent
db\models\query.py:445:80: E501 line too long (83 > 79 characters)
db\models\query.py:467:80: E501 line too long (113 > 79 characters)
db\models\query.py:469:17: E127 continuation line over-indented for visual indent
db\models\query.py:482:17: E127 continuation line over-indented for visual indent
db\models\query.py:495:17: E127 continuation line over-indented for visual indent
db\models\query.py:523:17: E127 continuation line over-indented for visual indent
db\models\query.py:553:17: E127 continuation line over-indented for visual indent
db\models\query.py:567:80: E501 line too long (84 > 79 characters)
db\models\query.py:581:21: E128 continuation line under-indented for visual indent
db\models\query.py:583:80: E501 line too long (103 > 79 characters)
db\models\query.py:585:17: E128 continuation line under-indented for visual indent
db\models\query.py:593:17: E127 continuation line over-indented for visual indent
db\models\query.py:595:17: E127 continuation line over-indented for visual indent
db\models\query.py:597:17: E128 continuation line under-indented for visual indent
db\models\query.py:633:21: E127 continuation line over-indented for visual indent
db\models\query.py:680:80: E501 line too long (80 > 79 characters)
db\models\query.py:681:21: E128 continuation line under-indented for visual indent
db\models\query.py:685:80: E501 line too long (90 > 79 characters)
db\models\query.py:699:80: E501 line too long (80 > 79 characters)
db\models\query.py:724:80: E501 line too long (80 > 79 characters)
db\models\query.py:734:80: E501 line too long (81 > 79 characters)
db\models\query.py:735:21: E128 continuation line under-indented for visual indent
db\models\query.py:744:17: E128 continuation line under-indented for visual indent
db\models\query.py:753:17: E127 continuation line over-indented for visual indent
db\models\query.py:764:17: E127 continuation line over-indented for visual indent
db\models\query.py:775:17: E127 continuation line over-indented for visual indent
db\models\query.py:777:80: E501 line too long (85 > 79 characters)
db\models\query.py:903:80: E501 line too long (85 > 79 characters)
db\models\query.py:920:80: E501 line too long (80 > 79 characters)
db\models\query.py:1013:80: E501 line too long (93 > 79 characters)
db\models\query.py:1014:21: E128 continuation line under-indented for visual indent
db\models\query.py:1018:80: E501 line too long (85 > 79 characters)
db\models\query.py:1039:21: E128 continuation line under-indented for visual indent
db\models\query.py:1043:80: E501 line too long (80 > 79 characters)
db\models\query.py:1054:21: E128 continuation line under-indented for visual indent
db\models\query.py:1079:80: E501 line too long (102 > 79 characters)
db\models\query.py:1201:17: E127 continuation line over-indented for visual indent
db\models\query.py:1240:1: E302 expected 2 blank lines, found 1
db\models\query.py:1245:80: E501 line too long (80 > 79 characters)
db\models\query.py:1303:80: E501 line too long (83 > 79 characters)
db\models\query.py:1304:80: E501 line too long (89 > 79 characters)
db\models\query.py:1305:80: E501 line too long (82 > 79 characters)
db\models\query.py:1307:80: E501 line too long (83 > 79 characters)
db\models\query.py:1323:80: E501 line too long (93 > 79 characters)
db\models\query.py:1323:91: E225 missing whitespace around operator
db\models\query.py:1330:80: E501 line too long (103 > 79 characters)
db\models\query.py:1332:80: E501 line too long (96 > 79 characters)
db\models\query.py:1333:80: E501 line too long (97 > 79 characters)
db\models\query.py:1332:94: E225 missing whitespace around operator
db\models\query.py:1336:80: E501 line too long (82 > 79 characters)
db\models\query.py:1359:80: E501 line too long (88 > 79 characters)
db\models\query.py:1361:29: E203 whitespace before ':'
db\models\query.py:1418:80: E501 line too long (82 > 79 characters)
db\models\query.py:1420:80: E501 line too long (92 > 79 characters)
db\models\query.py:1425:80: E501 line too long (85 > 79 characters)
db\models\query.py:1426:80: E501 line too long (88 > 79 characters)
db\models\query.py:1439:9: E128 continuation line under-indented for visual indent
db\models\query.py:1439:9: E125 continuation line does not distinguish itself from next logical line
db\models\query.py:1443:80: E501 line too long (87 > 79 characters)
db\models\query.py:1449:80: E501 line too long (80 > 79 characters)
db\models\query.py:1485:80: E501 line too long (81 > 79 characters)
db\models\query.py:1489:80: E501 line too long (82 > 79 characters)
db\models\query.py:1527:80: E501 line too long (84 > 79 characters)
db\models\query.py:1530:17: E128 continuation line under-indented for visual indent
db\models\query.py:1531:17: E128 continuation line under-indented for visual indent
db\models\query.py:1532:17: E128 continuation line under-indented for visual indent
db\models\query.py:1589:15: E261 at least two spaces before inline comment
db\models\query.py:1596:25: E261 at least two spaces before inline comment
db\models\query.py:1599:22: E261 at least two spaces before inline comment
db\models\query.py:1600:33: E261 at least two spaces before inline comment
db\models\query.py:1639:80: E501 line too long (83 > 79 characters)
db\models\query.py:1640:80: E501 line too long (83 > 79 characters)
db\models\query.py:1642:80: E501 line too long (92 > 79 characters)
db\models\query.py:1645:80: E501 line too long (89 > 79 characters)
db\models\query.py:1647:80: E501 line too long (82 > 79 characters)
db\models\query.py:1653:80: E501 line too long (81 > 79 characters)
db\models\query.py:1654:80: E501 line too long (81 > 79 characters)
db\models\query.py:1659:63: E225 missing whitespace around operator
db\models\query.py:1663:80: E501 line too long (93 > 79 characters)
db\models\query.py:1722:80: E501 line too long (80 > 79 characters)
db\models\query.py:1745:80: E501 line too long (83 > 79 characters)
db\models\query.py:1757:80: E501 line too long (81 > 79 characters)
db\models\query.py:1788:80: E501 line too long (82 > 79 characters)
Not so bad, after all we are investigating a file that has almost 2000 lines (many comments). Analysis of different classes in average gives more or less same results. The bigger the class the worst results.

Tuesday, July 31, 2012

Python and a GIL

I run recently into a very nice article that describes in an excellent way of what a GIL is and what it is not. So if you, as me run in to many GIL battles in your life, it is a good read. One thing to add, traditionally threads were introduced to do a parallel IO not a parallel computation, keep that in mind.