JSViews Merge id with string to get unique id - jsviews

I have an array which I am iterating over using
{{for}}
in the loop, I am creating various elements, one of which I need to generate unique Ids for include one of the variables in the array (Id)
so for example:
<div id="post-123">...
I have tried:
<div data-link="id{post-:Id}">...
and
div data-link="id{'post-':Id}">...
and
<div id="post-" data-link="id{merge:Id}">...
however none of these work.
if I omit the string and just use:
<div data-link="id{:Id}">...
it sets the Id just fine. Can anyone see what I am doing wrong?

These links talk of data-linking to attributes:
http://www.jsviews.com/#linked-elem-syntax
http://www.jsviews.com/#link-elemattribs
http://www.jsviews.com/#samples/data-link/attributes
The standard syntax is
data-link="attributeName{:dataPathOrExpression}"
In your case attributeName is id.
dataPathOrExpression can be any expression, so here you need it to be the Id value concatenated with (preceded by) the string 'post-', so you need to write:
<div data-link="id{:'post-' + Id}">...
or, equivalently
<div data-link='id{:"post-" + Id}'>...
You don't want to put anything between the { and :. The tag is {: (http://www.jsviews.com/#assigntag) - and the only thing you can put between those characters is a converter name such as myCvt, as in: id{myCvt:...}.
That said, if your Id values are not changing observably, then you don't need to data-link the id and you can instead write:
<div id="post-{{:Id}}">...
just as you would if you were rendering the template as a JsRender template, without data binding.

Related

Gremlin hasId using property of parent

I would like to filter out by an id. The value of the id is on a property of the parent. Example:
g.V('1234')
.as('parent')
.out()
.has(id, parent.childId). <--- how do I get the parent id?
How would one approach this?
The closest I've been able to get is the following query, but it seems to ignore the has and simply returns all results of out.
g.V('123')
.as('parent')
.values('childId')
.as('childIdValue')
.select('parent')
.out('hasChild')
.has('id', select('childIdValue'))
EDIT
It works as expected when a vertex property other than the Id is used. Example:
g.V('123')
.as('parent')
.values('childId')
.as('childIdValue')
.select('parent')
.out('hasChild')
.has('childProp', select('childIdValue'))
I've tried all variations of id that I can think of. 'id', id, id().
You can compare an ID using a where by approach. For example something like:
where(eq('parent')).by('childProp').by('childId')

Update dictionary key inside list using map function -Python

I have a dictionary of phone numbers where number is Key and country is value. I want to update the key and add country code based on value country. I tried to use the map function for this:
print('**Exmaple: Update phone book to add Country code using map function** ')
user=[{'952-201-3787':'US'},{'952-201-5984':'US'},{'9871299':'BD'},{'01632 960513':'UK'}]
#A function that takes a dictionary as arg, not list. List is the outer part
def add_Country_Code(aDict):
for k,v in aDict.items():
if(v == 'US'):
aDict[( '1+'+k)]=aDict.pop(k)
if(v == 'UK'):
aDict[( '044+'+k)]=aDict.pop(k)
if (v == 'BD'):
aDict[('001+'+k)] =aDict.pop(k)
return aDict
new_user=list(map(add_Country_Code,user))
print(new_user)
This works partially when I run, output below :
[{'1+952-201-3787': 'US'}, {'1+1+1+952-201-5984': 'US'}, {'001+9871299': 'BD'}, {'044+01632 960513': 'UK'}]
Notice the 2nd US number has 2 additional 1s'. What is causing that?How to fix? Thanks a lot.
Issue
You are mutating a dict while iterating it. Don't do this. The Pythonic convention would be:
Make a new_dict = {}
While iterating the input a_dict, assign new items to new_dict.
Return the new_dict
IOW, create new things, rather than change old things - likely the source of your woes.
Some notes
Use lowercase with underscores when defining variable names (see PEP 8).
Lookup values rather than change the input dict, e.g. a_dict[k] vs. a_dict.pop(k)
Indent the correct number of spaces (see PEP 8)

Need to reverse a repeat region

I have a firebase element that is pulling in the last 5 items
<firebase-element id="base" location="https://mydb.firebaseio.com/mydata" data="{{items}}" keys="{{keys}}" limit="5" ></firebase-element>
That is bound to this repeat region
<template repeat="{{id in keys}}">
<x-chat-list id="chatList" username="{{items[id]['uuid']}}" text="{{items[id]['text']}}" ></x-chat-list>
</template>
I simply need to reverse the order of the repeat region.
I think you just need to observe the array and reverse it (or store the reversed values in another attribute)
Ex :
keysChanged : function() { this.keys.reverse(); }
If the firebase-element sometimes changed the values in the array without triggering the keysChanged function, you can also use the observe object
Just add a custom Polymer expression, e.g.:
PolymerExpressions.prototype.revArray = function(a) {
return a.reverse();
}
and pipe your data to it:
<template repeat="{{id in keys | revArray}}">
Now you can use this anywhere since the expression handler is global.

Xquery group by on 2 tags

Below is the XML part of my data.
<A>
<a><Type>Fruit</Type><Name>Banana</Name></a>
<a><Type>Fruit</Type><Name>Orange</Name></a>
<a><Type>Fruit</Type><Name>Apple</Name></a>
<a><Type>Fruit</Type><Name>Lemon</Name></a>
<a><Type>Cars</Type><Name>Toyota</Name></a>
<a><Type>Cars</Type><Name>Lamborghini</Name></a>
<a><Type>Cars</Type><Name>Renault</Name></a>
</A>
Out put as -
<a>Fruits-Banana,Orange,Apple,Lemon</a>
<a>Cars-Toyota,Lamborghini,Renault</a>
I tried to get the required output by all in vain. I tried 'group by` clause too, but getting errors.
any help?
let $x:=
<A>
<a><Type>Fruit</Type><Name>Banana</Name></a>
<a><Type>Fruit</Type><Name>Orange</Name></a>
<a><Type>Fruit</Type><Name>Apple</Name></a>
<a><Type>Fruit</Type><Name>Lemon</Name></a>
<a><Type>Cars</Type><Name>Toyota</Name></a>
<a><Type>Cars</Type><Name>Lamborghini</Name></a>
<a><Type>Cars</Type><Name>Renault</Name></a>
</A>
for $z in distinct-values($x//a/Type)
let $c := $x//a[Type=$z]/Name
return
<a>{concat($z, "-", string-join($c, ","))}</a>
First for is taking the distinct values of the tag Type, then for each distinct value of this, the respective values of all the Name tags are derived.
Then using the concat function I have concatenated the Type text with the string generated by string-join, used to add/append the Name and , (comma).
HTH :)

Catalog Result By Positional Order in Contents

I want to have catalog result displayed using the following codes from my Archetypes package. The issue is the displayed listing not according to the positional order in Contents tab. What am I missing?
class BookView(BrowserView):
implements(IBookView)
def get_chapters(self):
context = aq_inner(self.context)
catalog = getToolByName(context, 'portal_catalog')
folder_path = '/'.join(context.getPhysicalPath())
results = catalog(
path={'query': folder_path, 'depth': 1},
portal_type=('File', 'Chapter')
)
return results
<div class="books-pdf"
tal:define="chapters view/get_chapters"
tal:condition="chapters">
<span class="listname"
i18n:translate="">Chapter Name</span>
<span class="iconname"
i18n:translate="">File Type</span>
<span class="sizename"
i18n:translate="">File Size</span>
<tal:chapters repeat="chapter chapters">
...
The catalog returns items in internal order unless you explicitly request an order. You can do so with the sort_on parameter, which should be the name of an index to use for the sorting.
Most indexes can be used for sorting, with the notable exception being full text indexes (which is why there is a sortable_title index in the Plone catalog).
You probably want to sort on the getObjPositionInParent index, which holds the index of each object in it's container:
results = catalog(
path=dict(query=folder_path, depth=1),
portal_type=('File', 'Chapter'),
sort_on='getObjPositionInParent',
)
You need to sort results by this index:
getObjPositionInParent

Resources