Import error for templated queries in Python using JinjaSql - python

So, I'm trying to create SQL templates with certain parameters would change based on the call. I reviewed many other methods but I found the method using JinjaSql more suitable and readable for the requirement of my module.
I tried creating a class as below (code shortened) to create a templated list of queries.
from jinjasql import JinjaSql
j = JinjaSql(param_style='pyformat')
class QueryTemplate(object):
def highest_pp_team_score(self, tour_name, year):
params = {'tour_name': [], 'year': []}
params['tour_name'].append(tour_name)
params['year'].append(year)
template = """
SELECT
id.match_id,
id.inning,
id.team
FROM
innings_deliveries as id
WHERE
id.name LIKE {{ tour_name }}
AND YEAR(id.start_date) = {{ year }}
"""
query, bind_params = j.prepare_query(template, params)
return query % bind_params
... when I try and run, I get the below error. It's on the import itself.
ImportError: cannot import name 'Markup' from 'jinja2.utils'
After reading about the issue, I couldn't find any resolutions to my case but I did find a thread with similar issue. It needed me to add few lines to the import. Since, the original issue was related to flask, I couldn't implement it to my case.

This is an issue in the library you are using. It has been reported on their issue tracker already, see https://github.com/sripathikrishnan/jinjasql/issues/50
What you could do is to downgrade your Jinja version to the one before 3.1.0 that brought those breaking changes of removing Markup and escape.
If you do have a requirements.txt file to install your dependencies, this is as simple as adding a line in it like
jinja2<3.1.0
And redo a
pip install --requirements requirements.txt
If you don't have such a file but are installing your packages with pip, you could do
pip install --upgrade 'jinja2<3.1.0'

Related

Understanding "Too many ancestors" from pylint

example.py:
'''
demo too many ancestors
'''
from flask_security.forms import RegisterForm
from wtforms.fields import TextField
class ExtendedRegisterForm(RegisterForm):
'''An extended register form'''
name = TextField('Name', [])
When I run pylint:
$ pylint -r n example.py
************* Module example
R: 7, 0: Too many ancestors (10/7) (too-many-ancestors)
What does this mean and how can I fix it?
The problem is that you inherit from a class which has itself (too) many ancestors: RegisterForm. In your case, you can't do a lot about this, beside stopping using it which is probably not an option. So you may want to disable this message for this class, eg:
class ExtendedRegisterForm(RegisterForm): # pylint: disable=too-many-ancestors
In addition to the disabling directives in the source code, you can configure this through the --max-parents= commandline option. You can also specify this in the config file (.pylintrc):
[DESIGN]
max-parents=15
As you can see I set it to 15 as many classes in Django (my project), particularly its view classes, will have hierarchies that are deeper than the default 7.
From documentation here: https://pylint.readthedocs.io/en/stable/technical_reference/features.html
too-many-ancestors (R0901): Too many ancestors (%s/%s) Used when
class has too many parent classes, try to reduce this to get a simpler
(and so easier to use) class.
basically Add to json or .pylintrc --max-parents='yourrequirednumber'
Note: --max-parents=7 is default
If using Visual Studio Code (VSC) you can easily override default config with adding
below code to settings.json
Go to --> UserSettings ctrl+, (default shortcut) --> click ... (more actions) --> open settings.json --> Add to overwrite Default settings this code:
"python.linting.pydocstyleArgs": ["--max-parents=25"]
NOTE2:
if pylint_django is not installed:
$ pip install pylint-django

How to handle apt installation which produce one more configuration dialog screens using python-apt?

For example when installing lirc package it gives configuration dialog screen. How to automate so as to select default values (or possible to provide spevific values) and proceed?
Right now i have a simple script:
#!/usr/bin/env python
import apt
import sys
pkg_name = "lirc"
cache = apt.cache.Cache()
cache.update()
pkg = cache[pkg_name]
if pkg.is_installed:
print "{pkg_name} already installed".format(pkg_name=pkg_name)
else:
pkg.mark_install()
try:
cache.commit()
except Exception, arg:
print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))
Configuration Window is as in below link
http://cdn.avsforum.com/d/db/db48d778_vbattach158986.jpeg
You can use
export DEBIAN_FRONTEND=noninteractive
and then use debconf to provide the values you have chosen.
This howto explains
If you want to supply an answer to a configuration question but do not want to be prompted for it then this can be arranged by preseeding the DebConf database with the required information. You will need to know:
the name of the package responsible for asking the question (which might not be the one you asked to install if there are dependencies),
the configuration database key against which the answer is recorded, and
the type of the answer (string, boolean, select, multiselect or password).
Configuration options can also be provided, in python-apt with the apt_pkg.Configuration class. By example
apt_pkg.config.set("Dir", self.tmpdir)
You then have to find the name of the options asked by the configuration script and set it via the python script.

Embed git hash into python file when installing

I want to embed the git hash into the version number of a python module if that module is installed from the git repository using ./setup.py install. How do I do that?
My thought was to define a function in setup.py to insert the hash and arrange to have it called when setup has copied the module to its build/lib/ directory, but before it has installed it to its final destination. Is there any way to hook into the build process at that point?
Edit: I know how to get the hash of the current version from the command line, I am asking about how to get such a command to run at the right time during the build/install.
Another, possibly simpler way to do it, using gitpython, as in dd/setup.py:
from pkg_resources import parse_version # part of `setuptools`
def git_version(version):
"""Return version with local version identifier."""
import git
repo = git.Repo('.git')
repo.git.status()
# assert versions are increasing
latest_tag = repo.git.describe(
match='v[0-9]*', tags=True, abbrev=0)
assert parse_version(latest_tag) <= parse_version(version), (
latest_tag, version)
sha = repo.head.commit.hexsha
if repo.is_dirty():
return f'{version}.dev0+{sha}.dirty'
# commit is clean
# is it release of `version` ?
try:
tag = repo.git.describe(
match='v[0-9]*', exact_match=True,
tags=True, dirty=True)
except git.GitCommandError:
return f'{version}.dev0+{sha}'
assert tag == f'v{version}', (tag, version)
return version
cf also the discussion at https://github.com/tulip-control/tulip-control/pull/145

how to set bug tracker url in setup.py script

I have just discovered the pypi web UI have a field 'Bug tracker URL' in edit of egg metadata.
This field exists so I guess it is supported in setup.py but I can't find anything about this using google.
So the question how do I set up this field in my setup.py so when doing a dist release on pypi it can be automaticly filled.
The entry is called bugtrack_url, but it's not being picked up from setup.py.
From context and code I understand it was intended to be used through-the-web on PyPI only, as per-project metadata, and not the usual per-release information.
The field is now considered a legacy field (hardcoded to None) and you instead add such information through the Project-URL list, which you can set in setuptools via the project_urls entry:
project_urls={
'Documentation': 'https://packaging.python.org/tutorials/distributing-packages/',
'Funding': 'https://donate.pypi.org',
'Say Thanks!': 'http://saythanks.io/to/example',
'Source': 'https://github.com/pypa/sampleproject/',
'Tracker': 'https://github.com/pypa/sampleproject/issues',
},
This option was finally added to setuptools in November 2017, and landed in version 38.3.0.
Bug tracker URL on PyPi project
In setup.py use project_urls in the setup :
setup(
...
project_urls={
'Documentation': 'https://readthedocs.io/',
'Funding': 'https://donate.pypi.org',
'Say Thanks!': 'http://saythanks.io/to/example',
'Source': 'https://github.com/pypa/sampleproject/',
'Tracker': 'https://github.com/pypa/sampleproject/issues',
},
...
)
The dict order is kept but listed in reversed on PyPi:
About PyPi bugtracker_url legacy code
pypa/warehouse Issue #233
bugtrack_url: IIRC it was something added by the PyPI maintainers to help projects, but in parallel PEP 345 introduced Project-URL which was intended to cover source code repository, bug tracker, mailing list, etc. If PEP 426 or one of its companion keeps Project-URL (and maybe improves it with defined labels for common sites, e.g. "repository"), then this special case becomes redundant.
And
At the moment, it looks like this is hardcoded to None in their API. I guess they left the field for backwards compatibility when they migrated...

pytest: Using dependency injection with decorators

At work we use a decorator #rollback on selected test functions which will rollback any db changes made during that test.
I've recently started using pytest's dependency injection for a few use cases, both with #pytest.mark.parametrize(...) and the pytest_funcarg__XXX hook. Unfortunately, this clashes with our decorated test functions.
How can I make this work?
My first idea was using a custom marker, say #pytest.mark.rollback and do something like:
def rollback(meth):
"""Original rollback function"""
...
def pytest_runtest_setup(item):
if not isinstance(item, pytest.Function):
return
if hasattr(item.obj, 'rollback'):
item = rollback(item)
Can Would an approach like this actually work?
Something like this should work fine, yes. Seems like you are using global state to manage your database, here, right? You might want to checkout the docs of the upcoming 2.3 release which also has a "transact" example further down this page:
http://pytest.org/dev/fixture.html
The release is due any time now and you can install the candidate with "pip install -i http://pypi.testrun.org -U pytest".

Categories

Resources