Category index for “python”

  • SQLALchemy vs Django ORM

    SQLALchemy vs Django ORM

    If you are working with Django ORM most of the time and then switching to SQLAlchemy - you may face some unexpected behavior. In this post I’ll try to describe the most important differences from my point of view.

    All examples for SQLAlchemy will be shown in async code, in context of PostgreSQL. Django version - 4.2, SQLAlchemy version - 2.0.

    The full examples can be found here https://github.com/st4lk/sqlalchemy-vs-django-orm, in the article the code will be cutted to be short.

    Comments Read More
  • Sublime Text and Language Server Protocol

    Sublime Text and Language Server Protocol (LSP)

    Language Server Protocol (LSP)

    LSP - protocol for interactions between IDE and language server. The latter provides such means like autocompletion, goto implementation and etc. When IDE needs to show autocomplete choices on, for example, python language - it sends a request to the special server. And it responds with the necessary data. The cool part here is that it is an initiative of a big company - Microsoft.

    Comments Read More
  • Python tips & tricks

    Python tips & tricks

    Recently i’ve read the book Learning Python, 5th Edition by Mark Lutz. Here is a list of most interesting insights for me.

    Comments Read More
  • Python logging for every day

    When writing a small python program or script, it is sometimes needed to output debug message or maybe event. It is known, that python has logging module exactly for that purpose. But in my case usually such thing happens: it is lack of time and hands just writes print instead of logging, because i can’t remember all those complicated logging settings. But then, if script is launched often or i must provide it to customer, i have to replace all prints with logging, because it is better to log all messages to file. So, not to keep in my head all these settings, i am writing this post.

    Comments Read More
  • Unicode string formatting

    Did you know, if one of values in string formatting expression with % operator is unicode, then result string will also be unicode?

    >>> "Hello, %s" % u"Alex"
    u'Hello, Alex'
    >>> "Hello, %s" % u"Алексей"
    u'Hello, \u0410\u043b\u0435\u043a\u0441\u0435\u0439'
    
    Comments Read More
  • Parse url which contains unicode query, using urlparse.parse_qs

    Task: get dictionary of URL GET query. For example, we have following url:

    http://example.com/?key=value&a=b
    

    it is needed to get a dict:

    {'key': ['value'], 'a': ['b']}
    

    Values are lists, because one key may have several values:

    In: http://example.com/?key=value&a=b&a=c
    Out: {'key': ['value'], 'a': ['b', 'c']}
    
    Comments Read More
  • Python function with mutable default arguments

    Python mutable default arguments

    In python default function arguments are created during executing instruction def and not at each function call. If argument value is immutable object (for example string, integer, tuple) it is ok, but if value is mutable, then there can be a trap:

    def foo(l=[]):
        l.append('x')
        return l
    

    It seems, that every call to foo() will return list [‘x’]. But:

    Comments Read More
  • Script for downloading music from vkontakte

    Script for downloading music from vkontakte

    A quick search of corresponding python script doesn’t give results. In post on habra link is broken. So i decided to write my own bicycle, it is avaliable here.

    Launch (needs installed python interpreter):

    python vkcom_audio_download.py
    

    Tested on python 2.6 and 2.7. No external libraries required.

    Comments Read More
  • MobileESP: Easily detect mobile web site visitors

    Script will be useful, if you want to show different version of site for desktop computers and mobile devices. Big variety of methods to detect mobile type. Avaliable in different languages, including python. The port to python was made by me with help from my freelance customer.

    Comments Read More
  • Debug django project with embedded python debugger pdb

    I use sublime-text as code editor. It doesn’t have a debugger, so to debug django projects i often used

    print var_name
    

    and look for output in local development server console. I use this approach today also, but sometimes it is great to run code step by step to see variables at each step.

    It can be done with embedded python debugger pdb:

    import pdb; pdb.set_trace()
    
    Comments Read More
  • Cloud service Openshift

    Cloud service Openshift

    I know a few hosting providers with free account and python availability. It is Google App Engine and Alwaysdata. But recently i found great project Openshift from RedHat and this blog site is working on it. Let me describe mentioned hostings first.

    Comments Read More