I have a dir with files. I need delete only some of them with condition.
For example... my folder contains:
super-lib-1.0.jar
super-lib-2.0.jar
super-lib-2.1.jar
cool-lib-3.3.1.jar
cool-lib-3.3.2.jar
I need delete only old versions of same lib. In this example I need delete files: super-lib-1.0.jar, super-lib-2.0.jar, cool-lib-3.3.1.jar
you can do it with javascript in ant like this way:
<?xml version="1.0" encoding="UTF-8"?>
<project default="init" name="My Project">
<scriptdef name="removeAllButLatest" language="javascript">
<attribute name="dir"/>
<![CDATA[
dir = new java.io.File(attributes.get("dir"));
files = dir.listFiles();
java.util.Arrays.sort(files, new java.util.Comparator({
compare: function(f1,f2) {
n1 = f1.getName();
n2 = f2.getName();
v1 = Number(n1.substring(n1.lastIndexOf('-')+1,n1.lastIndexOf('.')).replace(/\./g, ""));
v2 = Number(n2.substring(n2.lastIndexOf('-')+1,n2.lastIndexOf('.')).replace(/\./g, ""));
n1 = n1.substring(0, n1.lastIndexOf('-'));
n2 = n2.substring(0, n2.lastIndexOf('-'));
if (n1 < n2) {return -1;}
if (n1 > n2) {return 1;}
if (v1 < v2) {return 1;}
if (v1 > v2) {return -1;}
return 0;
}
}));
echo = project.createTask( "echo" );
name = "";
for(i = 0; i < files.length; i++) {
n = files[i].getName();
n = n.substring(0, n.lastIndexOf('-'));
if(n == name){
echo.setMessage( "deleting file: " + files[i] );
echo.perform();
files[i].delete();
}
else {
name = n;
}
}
]]>
</scriptdef>
<target name="init">
<removeAllButLatest dir="/home/guest/Desktop" />
</target>
</project>
Related
Currently using Dart with gsheets_api, which don't seem to have a function to convert column letters to numbers (column index)
As an example , this is what I use with AppScript (input: column letter, output: column index number):
function Column_Nu_to_Letter(column_nu)
{
var temp, letter = '';
while (column_nu > 0)
{
temp = (column_nu - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column_nu = (column_nu - temp - 1) / 26;
}
return letter;
};
This is the code I came up for Dart, it works, but I am sure there is a more elegant or correct way to do it.
String colLetter = 'L'; //Column 'L' as example
int c = "A".codeUnitAt(0);
int end = "Z".codeUnitAt(0);
int counter = 1;
while (c <= end) {
//print(String.fromCharCode(c));
if(colLetter == String.fromCharCode(c)){
print('Conversion $colLetter = $counter');
}
counter++;
c++;
}
// this output L = 12
Do you have any suggestions on how to improve this code?
First we need to agree on the meaning of the letters.
I believe the traditional approach is "A" is 1, "Z" is 26, "AA" is 27, "AZ" is 52, "BA" is 53, etc.
Then I'd probably go with something like these functions for converting:
int lettersToIndex(String letters) {
var result = 0;
for (var i = 0; i < letters.length; i++) {
result = result * 26 + (letters.codeUnitAt(i) & 0x1f);
}
return result;
}
String indexToLetters(int index) {
if (index <= 0) throw RangeError.range(index, 1, null, "index");
const _letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (index < 27) return _letters[index - 1];
var letters = <String>[];
do {
index -= 1;
letters.add(_letters[index.remainder(26)]);
index ~/= 26;
} while (index > 0);
return letters.reversed.join("");
}
The former function doesn't validate that the input only contains letters, but it works correctly for strings containing only letters (and it ignores case as a bonus).
The latter does check that the index is greater than zero.
A simplified version base on Irn's answer
int lettersToIndex(String letters) =>
letters.codeUnits.fold(0, (v, e) => v * 26 + (e & 0x1f));
String indexToLetters(int index) {
var letters = '';
do {
final r = index % 26;
letters = '${String.fromCharCode(64 + r)}$letters';
index = (index - r) ~/ 26;
} while (index > 0);
return letters;
}
I have the following raw SQL query:
UPDATE mySock s1
LEFT JOIN mySock s2
ON s1.parentId = s2.id
SET
s1.status = 1
s1.mylevel = (s2.mylevel + 1),
s1.parentString = CONCAT(s2.parentString, ':' CONCT(s1.id as char))
WHERE
s1.zz = 0;
and in create in Symfony 3.2
public function updateParentNew($idParent)
{
return $this->createQueryBuilder('s1')
->update('MyBundle:Stock', 's1')
->leftJoin(''MyBundle:Stock', 's2', 'WITH', 's2.id = s1.parentId')
->set('s1.zz', 1)
->set('s1.leveltask', 's2.leveltask + 1')
->set('s1.parentString', '?par2_string')
->where('s1.zz = 0')
->andWhere('s1.parentId = ?par1')
->setParameter('par1', $idParent)
->setParameter('par2_string', s2.parentString + ':' + (string)s1.id)
->getQuery()
->getSingleScalarResult();
}
It doesn't work. What is the way to Concat values (string and number)?
I would try something like this:
public function updateParentNew($idParent)
{
$qb = $this->createQueryBuilder('s1')
->update('MyBundle:Stock', 's1')
->leftJoin('MyBundle:Stock', 's2', 'WITH', 's2.id = s1.parentId')
;
$qb->set('s1.zz', 1)
->set('s1.leveltask', $qb->expr()->sum('s2.leveltask', 1))
->set('s1.parentString',
$qb->expr()->concat(
$qb->expr()->literal('s2.parentString'),
$qb->expr()->concat(':', $qb->expr()->literal('s1.id'))
))
->where('s1.zz = 0')
->andWhere('s1.parentId = :par1')
->setParameter('par1', $idParent)
->getQuery()
->getSingleScalarResult()
;
return $qb;
}
We develop a computationally-intensive web app in Dart.
For the purposes of making sure our computations perform well across all platforms and browsers, we run benchmarks using the "test 0.12.15+3" package. However, we found out that in some cases, the difference in executing the same code differs by orders of magnitude. Below is the code that demonstrates the problem (sorting arrays using custom comparators).
I wonder if there is an explanation for the difference in time between the code being executed in chrome, and as test with choice of chrome as platform (pub run test -p chrome test/perf_test.dart) ?
Is benchmarking via the test package a viable option, or should we look elsewhere?
import 'package:quiver/iterables.dart' as it;
void main(){
Stopwatch sw = new Stopwatch();
int len = 1000000;//0000
num v = 0;
List ln0 = new List();
for(int i = 0; i < len; i++)
ln0.add(v++);
//T1
sw..reset()..start();
ln0.sort((x,y) => x - y);
sw..stop();
print('Num Comparator take, mcs: ${sw.elapsedMicroseconds}');
//T2
List ln1= it.range(len).toList();
sw..reset()..start();
ln1.sort((x,y) => x - y);
print('Suddenly, Num Comparator take, mcs: ${sw.elapsedMicroseconds}');
//T3
List li2 = it.range(len).map((numm)=>numm.toInt()).toList();
sw..reset()..start();
li2.sort((x,y)=>x - y);
sw..stop();
print('Int Comparator take, mcs: ${sw.elapsedMicroseconds}');
List<String> items = new List(len);
for(int i =0, len = items.length; i<len; i++){
List tl = ['a','b','c','d','e','f','g','h','i','j','']..shuffle();
items[i] = tl.join('');
}
Function _comparer = (String s1, String s2) {
if (s1 == null && s2 == null) return 0;
if (s1 == null) return 1;
if (s2 == null) return -1;
if (s1 == '' && s2 == '') return 0;
if (s1 == '') return 1;
if (s2 == '') return -1;
return s1.compareTo(s2);
};
//T4
List ls1 = new List.from(items);
sw..reset()..start();
ls1.sort((s1, s2) => s1.compareTo(s2));
sw..stop();
print('Standart String comparator take, mcs: ${sw.elapsedMicroseconds}');
//T5
List ls2 = new List.from(items);
sw..reset()..start();
ls2.sort(_comparer);
sw..stop();
print('String comparator(miss null,empty) take, mcs: ${sw.elapsedMicroseconds}');
}
test is not supposed to be used for benchmarks. Use insted https://pub.dartlang.org/packages/benchmark_harness. It helps to cope with benchmark related requirements like warmup phase and number of runs to get more realistic results.
I need a way to call an Ant target if a given version (with dots) is greater than another version. I found greaterThan in ant-contrib, but I think that it only uses a straight string comparison unless the strings are completely numeric. For instance, I need something like "8.2.10" greaterThan "8.2.2" to evaluate to true. Is there anything in ant-contrib I can use, or has anyone ever written a custom script to do this?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="test" name="test">
<target name="script-definition">
<scriptdef name="greater" language="javascript">
<attribute name="v1"/>
<attribute name="v2"/>
<![CDATA[
self.log("value1 = " + attributes.get("v1"));
self.log("value2 = " + attributes.get("v2"));
var i, l, d, s = false;
a = attributes.get("v1").split('.');
b = attributes.get("v2").split('.');
l = Math.min(a.length, b.length);
for (i=0; i<l; i++) {
d = parseInt(a[i], 10) - parseInt(b[i], 10);
if (d !== 0) {
project.setProperty("compare-result", (d > 0 ? 1 : -1));
s = true;
break;
}
}
if(!s){
d = a.length - b.length;
project.setProperty("compare-result", (d == 0 ? 0 : (d > 0 ? 1 : -1)));
}
]]>
</scriptdef>
</target>
<target name="test" depends="script-definition">
<greater v1="8.2.2.1" v2="8.2.2.1.1.101" />
<echo message="compare-result: ${compare-result}" />
</target>
</project>
p.s: javascript cheated from here from Lejared's answer.
Pretty old post but there is the solution using the scriptlet task :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="test" name="test">
<scriptdef name="versioncompare" language="javascript">
<attribute name="arg1"/>
<attribute name="arg2"/>
<attribute name="returnproperty"/>
<![CDATA[
importClass(java.lang.Double);
var num1 = Double.parseDouble(attributes.get("arg1"));
var num2 = Double.parseDouble(attributes.get("arg2"));
project.setProperty(attributes.get("returnproperty"), (num1 > num2 ? 1 : (num1 < num2 ? -1 : 0)));
]]>
</scriptdef>
<target name="test">
<versioncompare arg1="2.0" arg2="1.9" returnproperty="compareresult"/>
<echo message="compareresult: ${compareresult}"/>
</target>
</project>
Is there any possible way to make a linked list in cypher within one transaction ?
Iv'e tried ForEach with Match but according to neo4jClien it is not possible to set Match in ForEach.
My approach :
public static void save(List<Post> nodes)
{
var gclient = graphdb.getConnection();
var create1 = gclient.Cypher.Create("(p:Post {nodes})");
var match = gclient.Cypher.Match("((t)-[r:lastPost]->(last))");
var create3 = gclient.Cypher.Create("t-[:lastPost]->p, p-[:next]->last");
var delete = gclient.Cypher.Delete("r");
string query = create1.Query.QueryText + " " + match.Query.QueryText + " "
+ create3.Query.QueryText + " " + delete.Query.QueryText;
gclient.Cypher
.Match("(t:Tmp)")
.WithParam("nodes", nodes)
.ForEach("(newPost in {nodes} | " + query + ")")
.ExecuteWithoutResults();
}
Thanks in advance .
this should do the trick.
static Neo4jClient.Cypher.ICypherFluentQuery addnode<T>(Neo4jClient.Cypher.ICypherFluentQuery q, IList<T> items, int idx, string label)
{
string sq = string.Format("({0}:{1} {{{2}}})", "c" + idx, label, "a" + idx);
q = q.Create(sq).WithParam("a" + idx, items[idx]);
return q;
}
static Neo4jClient.Cypher.ICypherFluentQuery addlink<T>(Neo4jClient.Cypher.ICypherFluentQuery q, int idx1, int idx2)
{
string sq = string.Format("{0}-[:LINKEDTO]->{1}", "c" + idx1, "c" + idx2);
q = q.Create(sq);
return q;
}
public static void Sample<T>(List<T> items, GraphClient client)
{
Neo4jClient.Cypher.ICypherFluentQuery q = client.Connection.Cypher;
for (int i = 1; i < items.Count; i++)
{
q = addnode<T>(q, items, i-1, "MYITEM");
if(i>1)
q = addlink<T>(q, i-2, i-1);
}
q.ExecuteWithoutResults();
}