The conventional way seems fine:
#view_config(route_name='new', renderer='new.mako')
return {'tasks': tasks}
But sometimes I may need to have fine control of what I am rendering, ie I may render different views subject to conditions. See this pseudocode:
#view_config(route_name='new')
def new_view(request):
if request.att == something:
one_dict = ...
a = render( "new.mako", one_dict)
else:
another_dict = ...
a = render( "new_special.mako", one_dict)
return a
How do I render an arbitary template myself with Pyramid's Mako engine and return it as a response?
You can use the render_to_response() renderer here:
Using the renderer renderer_name (a template or a static renderer), render the value (or set of values) using the result of the renderer's __call__ method (usually a string or Unicode) as the response body.
For your code that'd be:
#view_config(route_name='new')
def new_view(request):
if request.att == something:
one_dict = ...
a = render_to_response("new.mako", one_dict)
else:
another_dict = ...
a = render_to_response("new_special.mako", one_dict)
return a
or perhaps:
#view_config(route_name='new')
def new_view(request):
if request.att == something:
renderer = "new.mako"
else:
renderer = "new_special.mako"
return render_to_response(renderer, values)
Related
I need to test this function with a unit test:
def nlp_extraction(texts, nlp=None):
extr = []
for doc in nlp.pipe([texts]):
extr.append(list([ent.label_, ent.text]) for ent in doc.ents)
extracao = [list(extr[i]) for i in range(len(extr))]
extracao = list(chain.from_iterable(extracao))
extracao = " ".join([item[1] for item in extracao])
return [texts, extracao]
I wrote, inicialy, this test and worked:
def test_nlp_extraction_entrada_correta():
nlp = loadModel('ner_extract_ingredients')
result_reference = ['xilitol', 'xilitol']
texts = 'xilitol'
result = nlp_extraction(texts, nlp)
assert result == result_reference
But in this test I need to load the model. As this is an unit test, I would like to mock the responses, thus load an external model can be disable. I am trying something like this (and a combination of the lines commented in the code):
def test_nlp_extraction_entrada_correta():
texts = 'xilitol'
doc = Mock(name="DOC")
ents = Mock(name="ENTS", label_='xilitol', text="xilitol")
doc.ents = [ents]
from nextmock import Mock
nlp = Mock()
nlp_mock = Mock()
nlp.with_args([texts]).returns([doc])
nlp_mock.pipe = nlp([texts])
# nlp_mock.pipe.with_args([texts]).returns(doc)
# nlp_mock.pipe = [Mock(return_value=doc)]
result = nlp_extraction(texts, nlp=nlp_mock)
assert result == result_reference
But an error always raise, saying that nlp.pipe([texts]) mock object is not iterable. So, I need to mock this part nlp.pipe([texts]) and return the doc object. How I can do this? Something I am missing in the proccess, can someone help me.
As Cpt.Hook said in comments, the solution was achieved using nlp.pipe.return_value = [doc].
I am using the following function to create an MDlist TwoLineIconListItem in a scrollview. What I would like to do is change the icon in another function. I thought something like x.icon = 'New_icon' might work but didn't. Not sure where to look to get the desired result.
def rule_list(self):
'''Query of all rules and generates a list view under the rule tab....not really working all the way yet'''
db.execute('''SELECT * from rules''')
self.rows = db.fetchall()
for r in self.rows:
self.rule = f'{self.cfg["host"]}:{self.cfg["port"]}/api/firewall/filter/getRule/{r[2]}'
rules = TwoLineIconListItem(
text=r[1],
secondary_text=r[2],
on_release=lambda x: threading.Thread(
target=self.rule_on_click, args=(x.secondary_text, x), daemon=True).start()
)
self.check = requests.get(url=self.rule, auth=(
self.key, self.secret), verify=False)
if self.check.status_code == 200:
check_rule = json.loads(self.check.text)
if check_rule['rule']['enabled'] == '1':
rules.add_widget(IconLeftWidget(
icon='checkbox-marked-circle-outline'
))
else:
rules.add_widget(IconLeftWidget(
icon='checkbox-blank-circle-outline'
))
self.root.ids.ruleList.add_widget(rules)
I solved this by using the following in the function that had the logic for assigning the correct icon.
x.children[0].children[0].icon = new_icon
My program does this:
Get the XML from my website
Run all the URLs
Get data from my web page (SKU, name, title, price, etc.) with requests
Get the lowest price from another website, by comparing the price with the same SKU with requests.
I'm using with lots of requests, on each def:
def get_Price (SKU):
check ='https://www.XXX='+SKU
r = requests.get(check)
html = requests.get(r.url)
bsObj = BeautifulSoup(html.content,'html.parser')
return Price
def get_StoreName (SKU):
check ='https://XXX?keyword='+SKU
r = requests.get(check)
html = requests.get(r.url)
bsObj = BeautifulSoup(html.content,'html.parser')
return storeName
def get_h1Tag (u):
html = requests.get(u)
bsObj = BeautifulSoup(html.content,'xml')
h1 = bsObj.find('h1',attrs={'itemprop':'name'}).get_text()
return h1
How can I reduce the number of requests or connections to the URL - and use with one request or one connection throughout the whole program ?
I assume this is a script with a group of methods you call in a particular order.
If so, this is a good use case for a dict. I would write a function that memorizes calls to URLs.
You can then reuse this function across your other functions:
requests_cache = {}
def get_url (url, format_parser):
if url not in requests_cache:
r = requests.get(url)
html = requests.get(r.url)
requests_cache[url] = BeautifulSoup(html.content, format_parser)
return requests_cache[url]
def get_Price (makat):
url = 'https://www.zap.co.il/search.aspx?keyword='+makat
bsObj = get_url(url, 'html.parser')
# your code to find the price
return zapPrice
def get_zapStoreName (makat):
url = 'https://www.zap.co.il/search.aspx?keyword='+makat
bsObj = get_url(url, 'html.parser')
# your code to find the store name
return storeName
def get_h1Tag (u):
bsObj = get_url(u, 'xml')
h1 = bsObj.find('h1',attrs={'itemprop':'name'}).get_text()
return h1
If you want to avoid a global variable, you can also set requests_cache as attribute of get_url or as a default argument in the definition. The latter would also allow you to bypass the cache by passing an empty dict.
Again, the assumption here is that you are running this code as a script periodically. In that case, the requests_cache will get cleared every time you run the program.
However, if this is part of a larger program, you would want to 'expire' the cache on a regular basis, otherwise you would get the same results every time.
This is a good use case for the requests-cache library. Example:
from requests_cache import CachedSession
# Save cached responses in a SQLite file (scraper_cache.sqlite), and expire after 6 minutes
session = CachedSession('scraper_cache.sqlite', expire_after=360)
def get_Price (SKU):
check ='https://www.XXX='+SKU
r = session.get(check)
html = session.get(r.url)
bsObj = BeautifulSoup(html.content,'html.parser')
return Price
def get_StoreName (SKU):
check ='https://XXX?keyword='+SKU
r = session.get(check)
html = session.get(r.url)
bsObj = BeautifulSoup(html.content,'html.parser')
return storeName
def get_h1Tag (u):
html = session.get(u)
bsObj = BeautifulSoup(html.content,'xml')
h1 = bsObj.find('h1',attrs={'itemprop':'name'}).get_text()
return h1
Aside: with or without requests-cache, using sessions is good practice whenever you're making repeated calls to the same host, since it uses connection pooling: https://docs.python-requests.org/en/latest/user/advanced/#session-objects
I am trying to add the required behavior to a CharFiled or TextField so I can store a list of lists and retrieve it as a list of lists again. I am not asking for a solution rather I would like to see an example where a subclassing of an already supported field type is done as I didn't find any in the documentation or the Internet.
Do I have to do it as explained in the documents for creating a custom type?
for example:
class mylistoflists(TextField):
if yes, then what do I have to assign to field_type?
Example code (see tests/fields.py for full example):
class ListField(TextField):
def db_value(self, value):
return ','.join(value) if value else ''
def python_value(self, value):
return value.split(',') if value else []
class Todo(TestModel):
content = TextField()
tags = ListField()
class TestCustomField(ModelTestCase):
requires = [Todo]
def test_custom_field(self):
t1 = Todo.create(content='t1', tags=['t1-a', 't1-b'])
t2 = Todo.create(content='t2', tags=[])
t1_db = Todo.get(Todo.id == t1.id)
self.assertEqual(t1_db.tags, ['t1-a', 't1-b'])
t2_db = Todo.get(Todo.id == t2.id)
self.assertEqual(t2_db.tags, [])
t1_db = Todo.get(Todo.tags == Value(['t1-a', 't1-b'], unpack=False))
self.assertEqual(t1_db.id, t1.id)
I have a method which needs to search an xml file which was parsed using XmlParser for an element by name and return it only if that element is an end node. For example:
class xmlTest extends GroovyTestCase {
def void test(){
def xmlBody = """
<rootElement>
<elementWithOneChild>
<endElement>Here is the end</endElement>
</elementWithOneChild>
<elementWithManyChildren>
<one>1</one>
<two>1</two>
<three>1</three>
</elementWithManyChildren>
</rootElement>"""
def parsedBody = new XmlParser().parseText(xmlBody)
def search1 = parsedBody.depthFirst().grep({it.name() == "elementWithOneChild"})
println search1[0].children().size()
def search2 = parsedBody.depthFirst().grep({it.name() == "endElement"})
println search2[0].children().size()
def search3 = parsedBody.depthFirst().grep({it.name() == "elementWithManyChildren"})
println search3[0].children().size()
}
}
My attempt to use Node.children().size() works except for the 1 to 1 case where an element contains one child element. In this case, search1.children().size() and search2.children().size() both return 1. Although, the size for elementWithManyChildren is 3. I am looking for some way to be able to tell an end node apart from an element with one child.
One way I have found to work is:
try{
search1[0].children().iterator().next().name()
}catch(e){
//If the next node does not have a name, it is an end node
}
But that solution just seems like a poor one.
might waste a couple of cycles but this should allow you to find terminal nodes
assert search1[0].'**'.size() == 2
assert search2[0].'**'.size() == 1
assert search3[0].'**'.size() == 4