I'm trying to use the intcomma to format my number in template, but it cannot work properly.
{%load humanize%}
{%blocktrans with val=myvalue|intcomma%}The number is {{val}}{%endblocktrans%}
After some searching, I found the django.utils.formats.number_format is not function. Hereunder is my testing:
corpweb#56944bf480d1:~$ ./manage.py shell
Python 3.4.4 (default, Feb 17 2016, 02:50:56)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import locale
>>> from django.utils.formats import number_format
>>> val=123456789
>>> number_format(val,force_grouping=True)
'123456789'
>>> locale.getlocale()
('en_US', 'UTF-8')
>>>
Is there have anything I setup wrong?
When rendering templates or using number_format outside of the Django app flow the translation module is not activated. Here are a few notes and instructions on how to turn on translation in custom management commands.
To make the shell example work we just need to activate the translation module as such:
(venv) $ ./manage.py shell
Python 3.6.4 (default, Mar 1 2018, 18:36:50)
>>> from django.utils.formats import number_format
>>> from django.utils import translation
>>> translation.activate('en-us')
>>> number_format(50000, force_grouping=True)
'50,000'
The key line above is: translation.activate('en-us')
Everything is ok with your setup I guess. Just set USE_L10N = True in your settings.py if it is set to False, as #Tim Schneider mentioned, and you better try it like this {{ val|intcomma }} as #Leonard2 mentioned and it must work. And also as it is mentioned here make sure:
To activate these filters, add 'django.contrib.humanize' to your INSTALLED_APPS setting.
Related
I tried to use both openslide and pyvips and my application doesn't find the necesary .dll. I think it is a problem of using both librarys.
I have read that pyvips has openslide embed but I can't find how to use it. The main purpose for this is to read Whole Slide Images and see the different levels and augmentations, and work with them.
I'd really appreciate your help! Thank you
Yes, pyvips usually includes openslide, so you can't use both together.
Use .get_fields() to see all the metadata on an image, for example:
$ python3
Python 3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> x = pyvips.Image.new_from_file("openslide/CMU-1.svs")
>>> x.width
46000
>>> x.height
32914
>>> x.get_fields()
['width', 'height', 'bands', 'format', 'coding', 'interpretation', 'xoffset', 'yoffset',
'xres', 'yres', 'filename', 'vips-loader', 'slide-level', 'aperio.AppMag', 'aperio.Date',
'aperio.Filename', 'aperio.Filtered', 'aperio.Focus Offset', 'aperio.ICC Profile',
'aperio.ImageID', 'aperio.Left', 'aperio.LineAreaXOffset', 'aperio.LineAreaYOffset',
...
pyvips will open base level of the image by default (the largest), use level= to pick other levels, perhaps:
>>> x = pyvips.Image.new_from_file("openslide/CMU-1.svs", level=2)
>>> x.width
2875
See the docs for details:
https://www.libvips.org/API/current/VipsForeignSave.html#vips-openslideload
I'm a bit lost on how to extract coordinates (Lat, Long) from a URL in Python.
Always I'll recive a url like this:
https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896
Where I need to extract the second set of this URL (in this case: 41.403781,2.1896) Just to say, that not always the first and second set of coords will be the same.
I know, that can be done with some regex, but I'm not good enough on it.
Here's how to do it with a regular expression:
import re
m = re.search(r'pll=(\d+\.\d+),(\d+\.\d+)', 'https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896')
print m.groups()
Result: ('41.403781', '2.1896')
You might want look at the module urlparse for a more robust solution.
urlparse has a functions "urlparse" and "parse_qs" for accessing this data reliably, as shown below
$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> u="""https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896"""
>>> import urlparse
>>> x=urlparse.urlparse(u)
>>> x
ParseResult(scheme='https', netloc='www.testweb.com', path='/cordi', params='', query='ll=41.403781,2.1896&z=17&pll=41.403781,2.1896', fragment='')
>>> x.query
'll=41.403781,2.1896&z=17&pll=41.403781,2.1896'
>>> urlparse.parse_qs(x.query)
{'ll': ['41.403781,2.1896'], 'z': ['17'], 'pll': ['41.403781,2.1896']}
>>>
I wrote two scripts: modbus_master.py and modbus_helpers.py.
modbus_helpers.py is just a bunch of raw functions I defined that I'm trying to call from modbus_master.py.
When I try to execute 'modbus_master.py' from the windows CLI this happens...
C:\Python27\modbus_simulator>modbus_master.py
Traceback (most recent call last):
File "C:\Python27\modbus_master.py", line 3, in <module>
import modbus_helpers
ImportError: No module named modbus_helpers
However,
If I go to python interactive mode and do this...
C:\Python27\modbus_simulator>python
Python 2.7.5 (default, May 15 2013, 22:43:36) MSC v.1500 32 bit (Intel) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import modbus_master
The code in modbus_master.py that calls modbus_helpers.py works just fine. So how do I bridge this gap here so that I can just do this and run the script without error?
C:\Python27\modbus_simulator>modbus_master.py
Code in modbus_master.py:
import sys
import json
import modbus_helpers
import os
def printRegsets():
print 'these register sets were piped in...\r\n'
regsetIndex = 0
for regset in registersetsList:
print str(regsetIndex) , ':', regset['Name']
regsetIndex = regsetIndex + 1
path = os.path.normpath('C:\Python27\modbus_simulator\export2.txt')
registersetsList = modbus_helpers.getRegisterSetFromACMExportFile(path)
printRegsets()
Found the solution to the problem. There was nothing wrong with the code. I missed the obvious...modbus_master.py must be in the same folder/directory as modbus_helpers.py for the 'import' statement to work.
I'm on python 3.3 and I have to test a method which use call from subprocess.py.
I tried:
subprocess.call = MagicMock()
with patch('subprocess.call') as TU_call:
but in debug mode I found that python call effectively subprocess.call
Works fine for me (Ubuntu 13.04, Python 3.3.1):
$ python3.3
Python 3.3.1 (default, Sep 25 2013, 19:29:01)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mock
>>> import subprocess
>>> result = subprocess.call('date')
Fri Jan 3 19:45:32 CET 2014
>>> subprocess.call = mock.create_autospec(subprocess.call, return_value='mocked!')
>>> result = subprocess.call('date')
>>> print(result)
mocked!
>>> subprocess.call.mock_calls
[call('date')]
I believe this question is about the usage of this particular mock package
General statements, unrelated to your direct question
Wrote this up before I understood that the question is specifically about the use of the python mock package.
One general way to mock functions is to explicitly redefine the function or method:
$ python3.3
Python 3.3.1 (default, Sep 25 2013, 19:29:01)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('date')
Fri Jan 3 19:23:25 CET 2014
0
>>> def mocked_call(*a, **kw):
... return 'mocked'
...
>>> subprocess.call = mocked_call
>>> subprocess.call('date')
'mocked'
The big advantage of this straightforward approach is that this is free of any package dependencies. The disadvantage is that if there are specific needs, all the decision making logic has to be coded manually.
As an example of mocking packages, FlexMock is available for both Python 2.7 and Python 3.* and its usage of overriding subprocess.call is discussed in this question
This work for subprocess.check_output in python3
#mock.patch('subprocess.check_output', mock.mock_open())
#mock.patch('subprocess.Popen.communicate')
def tst_prepare_data_for_matrices(self, makedirs_mock, check_output_mock):
config_file = open(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)+'/etc/test/config.json')).read()
check_output_mock.return_value = ("output", "Error")
I want to use django-mailer without PINAX. When I run ./manager.py send_mail
it prints:
Unknown command: 'send_mail'
Type 'manage.py help' for usage.
How do I fix this?
Python 2.5.1 (r251:54863, Sep 22 2007, 01:43:31)
[GCC 4.2.1 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> 'mailer' in settings.INSTALLED_APPS
True
>>>
$./manage.py send_mail
Unknown command: 'send_mail'
Type 'manage.py help' for usage.
and I used easy_install django-mailer to install the mailer, and the django version
is
VERSION = (1, 1, 1, 'final', 0)
and mailer version is 0.1.0
A few things to double check:
Did you install django-mailer?
Is mailer in your PYTHONPATH? When you import mailer, are you getting the expected module (version and expected path)?
Is mailer listed in your INSTALLED_APPS?
$ ./manage.py shell
>>> import mailer
>>> mailer.get_version()
'0.1.0'
>>> mailer.__file__
/PATH/TO/YOUR/PYTHON/LIBS/mailer/__init__.py
>>> # did it import? did you get the expected version? expected path?
>>> # good, django-mailer is in your PYTHONPATH. now verify project settings.
>>> from django.conf import settings
>>> 'mailer' in settings.INSTALLED_APPS
True
At this point you should see send_mail in the list of available manage.py subcommands.
$ ./manage.py --help
Usage: manage.py subcommand [options] [args]
[...]
runserver
send_mail
shell
[...]
$
After than you will also want to make sure that you are running ./manage.py send_mail via a cron job.
* * * * * (cd $YOUR_PROJECT; /usr/bin/python manage.py send_mail >> cron_mail.log 2>&1)
0,20,40 * * * * (cd $YOUR_PROJECT; /usr/bin/python manage.py retry_deferred >> cron_mail_deferred.log 2>&1)
There is no need to actually set these two cronjobs up during development, just look for your messages via the admin.
The django-mailer module has usage instructions but this should get you up and running.
Can't you just download it from django-mailer and install it separately?