template.builtins.append(register) not working on django 1.8 but it was working on django1.6 - django-1.8

have query on following code which is working on django 1.6
from django import template register = template.Library()
#register.filter def lt(a,b): return float(a) < float(b); #register.filter def gt(a,b): return float(a) > float(b);
template.builtins.append(register)
but same code is not working on django 1.8 and throwing below error
template.builtins.append(register) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'builtins'
why it is throwing error? if any one has already got this issue ,provide your input?

Related

Methods after has_one_attached don't work

I am using Active Storage to store images but and doesn't work for a specific model, first an example with a working model:
class GalleryImage < ApplicationRecord
has_one_attached :image
end
In this case everything works well, here is the console output:
2.7.0 :006 > ::GalleryImage.new.image
=> #<ActiveStorage::Attached::One:0x0000557a5b57b1f8 #name="image" ...>>
But, I have another model called AppSetting like this:
class AppSetting < ApplicationRecord
def hola
'hola'
end
has_one_attached :gimmick
def adios
'adios'
end
end
And, when I try it in the rails console:
2.7.0 :006 > ::AppSetting.new.hola
=> "hola"
2.7.0 :007 > ::AppSetting.new.gimmick
Traceback (most recent call last):
1: from (irb):7
NoMethodError (undefined method `gimmick' for #<AppSetting:0x0000560cb955b470>)
2.7.0 :008 > ::AppSetting.new.adios
Traceback (most recent call last):
2: from (irb):7
1: from (irb):8:in `rescue in irb_binding'
NoMethodError (undefined method `adios' for #<AppSetting:0x0000560cb94241d8>)
As you can see, the first method (hola) works well, but I'm getting NoMethodError (undefined method... error message for the next methods.
any help? please.

Attribute error while creating web page with justpy

Hi I was creating a simple plain web page with justpy and I got an attribute error.
This was the code:
import justpy as jp
def app():
wp = jp.WebPage()
return wp
jp.justpy(app)
this was the error:
/usr/local/lib/python3.6/dist-packages/justpy
Module directory: /usr/local/lib/python3.6/dist-packages/justpy, Application directory: /home/nsu/Desktop/datavisual
Traceback (most recent call last):
File "Web_App_1.py", line 7, in <module>
jp.justpy(app)
File "/usr/local/lib/python3.6/dist-packages/justpy/justpy.py", line 383, in justpy
uvicorn.run(app, host=host, port=port, log_level=UVICORN_LOGGING_LEVEL)
File "/usr/local/lib/python3.6/dist-packages/uvicorn/main.py", line 461, in run
server.run()
File "/usr/local/lib/python3.6/dist-packages/uvicorn/server.py", line 67, in run
return asyncio.run(self.serve(sockets=sockets))
AttributeError: module 'asyncio' has no attribute 'run'
I tried to replace jp.WebPage() with jp.QuasarPage() but got the same error
pls help.

Unit test for exception raised in custom GNU radio python block

I have created a custom python sync block for use in a gnuradio flowgraph. The block tests for invalid input and, if found, raises a ValueError exception. I would like to create a unit test to verify that the exception is raised when the block indeed receives invalid input data.
As part of the python-based qa test for this block, I created a flowgraph such that the block receives invalid data. When I run the test, the block does appear to raise the exception but then hangs.
What is the appropriate way to test for this? Here is a minimal working example:
#!/usr/bin/env python
import numpy as np
from gnuradio import gr, gr_unittest, blocks
class validate_input(gr.sync_block):
def __init__(self):
gr.sync_block.__init__(self,
name="validate_input",
in_sig=[np.float32],
out_sig=[np.float32])
self.max_input = 100
def work(self, input_items, output_items):
in0 = input_items[0]
if (np.max(in0) > self.max_input):
raise ValueError('input exceeds max.')
validated_in = output_items[0]
validated_in[:] = in0
return len(output_items[0])
class qa_validate_input (gr_unittest.TestCase):
def setUp (self):
self.tb = gr.top_block ()
def tearDown (self):
self.tb = None
def test_check_valid_data(self):
src_data = (0, 201, 92)
src = blocks.vector_source_f(src_data)
validate = validate_input()
snk = blocks.vector_sink_f()
self.tb.connect (src, validate)
self.tb.connect (validate, snk)
self.assertRaises(ValueError, self.tb.run)
if __name__ == '__main__':
gr_unittest.run(qa_validate_input, "qa_validate_input.xml")
which produces:
DEPRECATED: Using filename with gr_unittest does no longer have any effect.
handler caught exception: input exceeds max.
Traceback (most recent call last):
File "/home/xxx/devel/gnuradio3_8/lib/python3.6/dist-packages/gnuradio/gr/gateway.py", line 60, in eval
try: self._callback()
File "/home/xxx/devel/gnuradio3_8/lib/python3.6/dist-packages/gnuradio/gr/gateway.py", line 230, in __gr_block_handle
) for i in range(noutputs)],
File "qa_validate_input.py", line 21, in work
raise ValueError('input exceeds max.')
ValueError: input exceeds max.
thread[thread-per-block[1]: <block validate_input(2)>]: SWIG director method error. Error detected when calling 'feval_ll.eval'
^CF
======================================================================
FAIL: test_check_valid_data (__main__.qa_validate_input)
----------------------------------------------------------------------
Traceback (most recent call last):
File "qa_validate_input.py", line 47, in test_check_valid_data
self.assertRaises(ValueError, self.tb.run)
AssertionError: ValueError not raised by run
----------------------------------------------------------------------
Ran 1 test in 1.634s
FAILED (failures=1)
The top_block's run() function does not call the block's work() function directly but starts the internal task scheduler and its threads and waits them to finish.
One way to unit test the error handling in your block is to call the work() function directly
def test_check_valid_data(self):
src_data = [[0, 201, 92]]
output_items = [[]]
validate = validate_input()
self.assertRaises(ValueError, lambda: validate.work(src_data, output_items))

python string import doesn't work

post#1 Learning python.
Using IDLE shell and script, both experience this issue.
Successfully importing "string" module, but calling a function from it returns an error:
from string import *
find("atgacatgcacaagtatgcat","atg")
error:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
find("atgacatgcacaagtatgcat","atgc")
NameError: name 'find' is not defined
find is a method of the class string. You have to use .find
try below example:
str1 = "atgacatgcacaagtatgcat"
str2 = "atg"
str1.find(str2)

html5lib: TypeError: __init__() got an unexpected keyword argument 'encoding'

I'm trying to install html5lib. at first I tried to install the latest version (8 or 9 nines), but it came into conflict with my BeautifulSoup, so I decided to try older verison (0.9999999, seven nines ). I installed it, but when I try to use it:
>>> with urlopen("http://example.com/") as f:
document = html5lib.parse(f, encoding=f.info().get_content_charset())
I get an error:
Traceback (most recent call last):
File "<pyshell#11>", line 2, in <module>
document = html5lib.parse(f, encoding=f.info().get_content_charset())
File "C:\Python\Python35-32\lib\site-packages\html5lib\html5parser.py", line 35, in parse
return p.parse(doc, **kwargs)
File "C:\Python\Python35-32\lib\site-packages\html5lib\html5parser.py", line 235, in parse
self._parse(stream, False, None, *args, **kwargs)
File "C:\Python\Python35-32\lib\site-packages\html5lib\html5parser.py", line 85, in _parse
self.tokenizer = _tokenizer.HTMLTokenizer(stream, parser=self, **kwargs)
File "C:\Python\Python35-32\lib\site-packages\html5lib\_tokenizer.py", line 36, in __init__
self.stream = HTMLInputStream(stream, **kwargs)
File "C:\Python\Python35-32\lib\site-packages\html5lib\_inputstream.py", line 151, in HTMLInputStream
return HTMLBinaryInputStream(source, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'encoding'
What is wrong and what should I do?
I see something was broken in the latest versions of html5lib in regard to bs4, html5lib.treebuilders._base is no longer there, usng bs4 4.4.1 the latest compatible version seems to be the one with 7 nines, once you install it as below it works fine:
pip3 install -U html5lib=="0.9999999"
Tested using bs4 4.4.1:
In [1]: import bs4
In [2]: bs4.__version__
Out[2]: '4.4.1'
In [3]: import html5lib
In [4]: html5lib.__version__
Out[4]: '0.9999999'
In [5]: from urllib.request import urlopen
In [6]: with urlopen("http://example.com/") as f:
...: document = html5lib.parse(f, encoding=f.info().get_content_charset())
...:
In [7]:
You can see the change in this commit Rename treebuilders._base to .base to reflect public status the name was changed:
The error you see is because you are still using the newest version, in html5lib/_inputstream.py, HTMLBinaryInputStream has no encoding arg:
class HTMLBinaryInputStream(HTMLUnicodeInputStream):
"""Provides a unicode stream of characters to the HTMLTokenizer.
This class takes care of character encoding and removing or replacing
incorrect byte-sequences and also provides column and line tracking.
"""
def __init__(self, source, override_encoding=None, transport_encoding=None,
same_origin_parent_encoding=None, likely_encoding=None,
default_encoding="windows-1252", useChardet=True):
Setting override_encoding=f.info().get_content_charset() should do the trick.
Also upgrading to the latest version of bs4 works fine with the latest version of html5lib:
In [16]: bs4.__version__
Out[16]: '4.5.1'
In [17]: html5lib.__version__
Out[17]: '0.999999999'
In [18]: with urlopen("http://example.com/") as f:
document = html5lib.parse(f, override_encoding=f.info().get_content_charset())
....:
In [19]:

Resources