How to fix "plural forms could be dangerous" django error? - python

I don't get it. If I set the Accept-Language header to anything other than "en" (de, pl, es) or even something that doesn't exist (like xxs), the app doesn't spit out this error, but it does when I set it to "en". It happens only on windows (newest gettext tools). Here's the stack trace:
Traceback (most recent call last):
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\core\han
dlers\exception.py", line 39, in inner
response = get_response(request)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\core\han
dlers\base.py", line 244, in _legacy_get_response
response = middleware_method(request)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\middlewa
re\locale.py", line 29, in process_request
translation.activate(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\__init__.py", line 161, in activate
return _trans.activate(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 238, in activate
_active.value = translation(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 227, in translation
_translations[language] = DjangoTranslation(language)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 129, in __init__
self._add_installed_apps_translations()
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 176, in _add_installed_apps_translations
translation = self._new_gnu_trans(localedir)
File "E:\projekty\python\myapp_api\_env\lib\site-packages\django\utils\tr
anslation\trans_real.py", line 156, in _new_gnu_trans
fallback=use_null_fallback)
File "C:\Python35\lib\gettext.py", line 426, in translation
t = _translations.setdefault(key, class_(fp))
File "C:\Python35\lib\gettext.py", line 162, in __init__
self._parse(fp)
File "C:\Python35\lib\gettext.py", line 297, in _parse
self.plural = c2py(plural)
File "C:\Python35\lib\gettext.py", line 76, in c2py
raise ValueError('plural forms expression could be dangerous')
ValueError: plural forms expression could be dangerous
I have plural-forms set up correctly in my django.po file:
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
Why does this happen and how to fix it?

I found an answer - the reason for that error is my incompetence :). I ran django-admin makemessages command in the root of my project, so next to _env and all the packages inside. This command created language files for a couple of projects with the default django.po file template, so they contained something like plural-forms=INTEGER and other stuff and this was causing the above error.

Here I will give you all the details about Plural-forms:
See this link Plural-forms.It will helps to you.
Note:This just link give a simple idea about plural
It surely helps to others

Your plural expression (n != 1) is most likely considered dangerous because it's too inclusive. n doesn't equal 1 means any value except 1 will evaluate to True including booleans, strings or None.
Try changing your expression to use equals, greater than and less than operations to narrow down the scope of the expression.

Related

Removing an entire tree in Neptune using Gremlin

I'm new to using Gremlin + Neptune. I'm writing a small python program to cleanup some edges + nodes in a Neptune DB. To start, I want to delete all nodes + edges rooted at a particular node.
After looking around, I came across How to delete all child nodes when parent node deleted using the gremlin query?, and I tried the following:
g.V().has("id", <insert id>).union(__(), __.repeat(__.out()).emit()).drop()
However, the node with "id" still exists in the DB. When I try running the above command straight on the python3 console, I get the following output:
>>> g.V().has("id", <insert id>).union(__(), __.repeat(__.out()).emit()).drop()
[['V'], ['has', 'id', '5c4266a3a44649ddb24ce6cf4f831300'], ['union', <gremlin_python.process.graph_traversal.__ object at 0x7fa51c967fd0>, [['repeat', [['out']]], ['emit']]], ['drop']]
What's going on here? It almost looks like it's listing out the steps it plans to take, but not executing them.
Edit -- From answer below, I added .iterate() to the end. I get this error now:
Traceback (most recent call last):
...
tg.g.V().has("id", <id>).union(__(), __.repeat(__.out()).emit()).drop().iterate()
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/process/traversal.py", line 66, in iterate
try: self.nextTraverser()
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/process/traversal.py", line 71, in nextTraverser
self.traversal_strategies.apply_strategies(self)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/process/traversal.py", line 573, in apply_strategies
traversal_strategy.apply(traversal)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/remote_connection.py", line 149, in apply
remote_traversal = self.remote_connection.submit(traversal.bytecode)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/driver_remote_connection.py", line 55, in submit
result_set = self._client.submit(bytecode)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/client.py", line 111, in submit
return self.submitAsync(message, bindings=bindings).result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/connection.py", line 66, in cb
f.result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/protocol.py", line 74, in write
request_id, request_message)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/serializer.py", line 132, in serialize_message
message = self.build_message(request_id, processor, op, args)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/serializer.py", line 142, in build_message
return self.finalize_message(message, b"\x21", self.version)
File "/home/vagrant/.cache/bazel/_bazel_vagrant/f9910d98673307a31f928c448bd4acd0/execroot/project/bazel-out/k8-fastbuild/bin/path/to/directory/python_code.runfiles/pip_deps/pypi__gremlinpython/gremlin_python/driver/serializer.py", line 145, in finalize_message
message = json.dumps(message)
File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type __ is not JSON serializable
Unlike the Gremlin Console, the Python Console doesn't automatically iterate your traversals for you. As in your application code you must iterate them yourself. When you do
g.V().has("id", <insert id>).union(__.identity(), __.repeat(__.out()).emit()).drop()
that simply spawns a Traversal object but does not execute it. You must therefore iterate it in some way to exhaust the elements within it - in your case, the appropriate terminator to use would be iterate():
g.V().has("id", <insert id>).union(__.identity(), __.repeat(__.out()).emit()).drop().iterate()
The semantics around drops in TinkerPop aren't always consistent unfortunately. TinkerPop tried to preserve flexibility for providers in how they implement that but it causes confusion sometimes because a query will work fine in TinkerGraph but then behave slightly differently when executed on a different provider. If the above approach only drops the root, you could try to realize the results before the drop:
g.V().has("id", <insert id>).
union(__.identity(),
__.repeat(__.out()).emit()).
fold().unfold().
drop()
Looks a bit dumb but will force all the vertices you wish to drop to be traversed into a list before they are dropped. In that way, you won't kill the repeat() by dropping the parent and its edges first and leaving the child paths untravelled.

Django/cookiecutter-django Error: "AttributeError: 'Path' object has no attribute 'endswith''" with manage.py makemigrations

I have been trying to set up Django with a cookiecutter-django project
with cookiecutter https://github.com/pydanny/cookiecutter-django.git
and I get this error output when I run ./manage.py makemigrations at the end.
My python version in the virturalenv is Python 3.5.2
This is the tail of console output and not from a file.
Stack overflow doesn't allow me to post all of the long error output without more of other text.
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/db/models/base.py", line 156, in __new__
new_class.add_to_class(obj_name, obj)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/db/models/base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 1588, in contribute_to_class
self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/db/models/fields/related.py", line 1057, in create_many_to_many_intermediary_model
'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/functional.py", line 192, in __mod__
return str(self) % rhs
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/functional.py", line 156, in __text_cast
return func(*self.__args, **self.__kw)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/translation/__init__.py", line 79, in gettext
return _trans.gettext(message)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 285, in gettext
_default = _default or translation(settings.LANGUAGE_CODE)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 198, in translation
_translations[language] = DjangoTranslation(language)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 99, in __init__
self._add_local_translations()
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 151, in _add_local_translations
translation = self._new_gnu_trans(localedir)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/site-packages/django/utils/translation/trans_real.py", line 123, in _new_gnu_trans
fallback=use_null_fallback,
File "/usr/lib/python3.5/gettext.py", line 428, in translation
mofiles = find(domain, localedir, languages, all=True)
File "/usr/lib/python3.5/gettext.py", line 396, in find
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
File "/root/painlesslanguage/painlesslanguageenv/lib/python3.5/posixpath.py", line 84, in join
elif not path or path.endswith(sep):
AttributeError: 'Path' object has no attribute 'endswith'
I left the TIME_ZONE cookiecutter-django setup option blank if that matters.
this seems to be a difficult error to fix or even debug.
some users have gotten this error when they have not included the static url properly in settings.py of left a trailing comma in the INSTALLED_APPS declaration.
however i got this error just when i switched a python version.
I ended up somehow solving it (at the time) by reinstalling python and relinking pyenv python to brew.
however, now its returned. there must be a way to debug this.
all the stacktrace points to files in the python install or only to the settings file. but if your settings file looks fine, its really hard to know how to fix it. After checking your settings.py INSTALLED_APPS (to check there are no trailing commas) id suggest reinstalling python and see if that helps.
if that doesnt work, then what helped me was printing out the apps being installed. to do this you will have to add print lines into the python/django code where the errors are thrown. as by default this info isnt being sent to the application layer, though it should be. these will be removed if you ever change the py install again. so its kind of a temperoray/local way to help u debug. really the framework should send the name info if it fails to import an app. by adding these lines i found what was going wrong with my import.

Mysterious "embedded null byte" error

Working on a fairly large/complex Django project with a team, we occasionally see runserver crash with ValueError: embedded null byte. We restart runserver and it's fine - either for a few minutes or a few days. We can detect no pattern to what causes the crashes (seems totally random). Fortunately it's only happening in local development, not on our servers, but I'm worried that it will bite us down the road.
The stack trace below does not point to any location in our code -- seems to come either from Django or the virtualenv itself.
Using Django 1.9.8, Python 3.5.0, on El Capitan.
I can't see any way to debug this. Theories?
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 54, in execute
super(Command, self).execute(*args, **options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 93, in handle
self.run(**options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 102, in run
autoreload.main(self.inner_run, None, options)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 333, in main
reloader(wrapped_main_func, args, kwargs)
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 299, in python_reloader
reloader_thread()
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 275, in reloader_thread
change = fn()
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 204, in code_changed
for filename in gen_filenames():
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 114, in gen_filenames
basedirs = [os.path.abspath(basedir) for basedir in basedirs
File "/path/to/virtualenvs/ourproj/lib/python3.5/site-packages/django/utils/autoreload.py", line 115, in <listcomp>
if os.path.isdir(basedir)]
File "/path/to/virtualenvs/ourproj/bin/../lib/python3.5/genericpath.py", line 42, in isdir
st = os.stat(s)
ValueError: embedded null byte
One of the AppConfig objects has null byte in its path attribute.
One of the LOCALE_PATHS has null byte.
One of the files is in the "wrong" encoding, so Django treats something has null byte (e. g. AppConfig.path).
One of the files or directories in project directory has null byte (\x00) in its name.
Other reason.
autoreload.py lines related to this issue. os.path.isdir() raises ValueError: embedded null byte if its argument has null byte, e. g. os.path.isdir('foo\x00bar').
You can try to edit autoreload.py, comment out these lines temporarily:
basedirs = [os.path.abspath(basedir) for basedir in basedirs
if os.path.isdir(basedir)]
and add this:
temp_basedirs = []
for basedir in basedirs:
try:
if os.path.isdir(basedir):
temp_basedirs.append(os.path.abspath(basedir))
except ValueError:
print(basedir)
raise
basedirs = temp_basedirs

Django - ValueError: plural forms expression could be dangerous

I'm working with Django 1.9 in Microsoft Visual Studio. Also I'm using a virtual env, where all the needed python packages are installed. When I try to compile messages in order to add translation function to the site, the following error is raised:
ValueError: plural forms expression could be dangerous
Update 1. Stacktrace:
Internal Server Error: /
Traceback (most recent call last):
File "C:\path-to-project\env\lib\site-packages\django\core\handlers\base.py", line 123, in get_response
response = middleware_method(request)
File "C:\path-to-project\env\lib\site-packages\django\middleware\locale.py", line 26, in process_request
translation.activate(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\__init__.py", line 154, in activate
return _trans.activate(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 216, in activate
_active.value = translation(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 205, in translation
_translations[language] = DjangoTranslation(language)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 114, in __init__
self._add_installed_apps_translations()
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 160, in _add_installed_apps_translations
translation = self._new_gnu_trans(localedir)
File "C:\path-to-project\env\lib\site-packages\django\utils\translation\trans_real.py", line 140, in _new_gnu_trans
fallback=use_null_fallback)
File "C:\Python27\Lib\gettext.py", line 481, in translation
t = _translations.setdefault(key, class_(fp))
File "C:\Python27\Lib\gettext.py", line 182, in __init__
self._parse(fp)
File "C:\Python27\Lib\gettext.py", line 318, in _parse
self.plural = c2py(plural)
File "C:\Python27\Lib\gettext.py", line 95, in c2py
raise ValueError, 'plural forms expression could be dangerous'
ValueError: plural forms expression could be dangerous
I've read about that (plural forms expression could be dangerous and Django: gettext raises ValueError: 'plural forms expression could be dangerous'), but the only solution I found is to remove lines referred to plural translations, for example:
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
If I remove those lines in my translations files, it still raises the same error, because it takes into account other files located at the virtual env.
Update 2: If I run the server from the command line, using
python manage.py runserver
there's no problem, everything is going right. However, when I run the project from Visual Studio, the error is raised.
Is there any way to solve this problem?
Thanks in advance.

Searching for a string with Web.py

I'm trying to build a python function with web.py and SQLite that will allow users to search for a given string within a description field and will return all matching results.
Right now I've gotten to the below function, which works but only if the input is an exact match.
def getItem(params, max_display):
query_string = 'SELECT * FROM items WHERE 1=1'
description = params['description']
if params['description']:
query_string = query_string + ' AND description LIKE $description'
result = query(query_string, {
'description': params['description']
I've tried to implement this feature with LIKE "%$description%"' , however I keep getting the below web.py error.
Traceback (most recent call last):
File "lib/web/wsgiserver/__init__.py", line 1245, in communicate
req.respond()
File "lib/web/wsgiserver/__init__.py", line 775, in respond
self.server.gateway(self).respond()
File "lib/web/wsgiserver/__init__.py", line 2018, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "lib/web/httpserver.py", line 306, in __call__
return self.app(environ, xstart_response)
File "lib/web/httpserver.py", line 274, in __call__
return self.app(environ, start_response)
File "lib/web/application.py", line 279, in wsgi
result = self.handle_with_processors()
File "lib/web/application.py", line 249, in handle_with_processors
return process(self.processors)
File "lib/web/application.py", line 246, in process
raise self.internalerror()
File "lib/web/application.py", line 478, in internalerror
return debugerror.debugerror()
File "lib/web/debugerror.py", line 305, in debugerror
return web._InternalError(djangoerror())
File "lib/web/debugerror.py", line 290, in djangoerror
djangoerror_r = Template(djangoerror_t, filename=__file__, filter=websafe)
File "lib/web/template.py", line 846, in __init__
code = self.compile_template(text, filename)
File "lib/web/template.py", line 926, in compile_template
ast = compiler.parse(code)
File "/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py", line 51, in parse
return Transformer().parsesuite(buf)
File "/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py", line 128, in parsesuite
return self.transform(parser.suite(text))
AttributeError: 'module' object has no attribute 'suite'
Any thoughts on what might be going wrong with this function?
Thanks in advance!
What do you think is going on with parser.py?
Here is the relevant portion of the error message:
File
"/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py",
line 128, in parsesuite
return self.transform(parser.suite(text)) AttributeError: 'module' object has no attribute 'suite'
So, somewhere there is a file called parser.py, which defines a function called suite(), which is used by some library code that executes when your program executes. But because you named one of your files parser.py, when the library code executes, python searches for a file named parser.py, and python found your file first, and there was no function named suite() in your file.

Categories

Resources