Skip to content

Instantly share code, notes, and snippets.

@vlcinsky
vlcinsky / date.xquery
Created July 18, 2011 23:09
filtering documents based on custom date format in XQuery
for $doc in //Document
let $datestr := $doc/@time
let $datearr := tokenize($datestr, '/')
let $year := $datearr[3]
let $month := format-number(xs:int($datearr[2]), '#00')
let $day := format-number(xs:int($datearr[1]), '#00')
let $dateisostr := concat($year, '-', $month, '-', $day)
where $dateisostr > '2010-06-01' and $dateisostr < '2011-01-01'
(:
let $date := xs:date($dateisostr)
@vlcinsky
vlcinsky / docs.xml
Created July 18, 2011 23:20
Source xml document, containing Document elements to be filtered by custom formated date in @time attribute
<Documents>
<Document id="1" time="5/2/2010"/>
<Document id="2" time="4/8/2011"/>
<Document id="3" time="6/9/2010"/>
<Document id="4" time="8/10/2010"/>
</Documents>
@vlcinsky
vlcinsky / returned-xml-elements.xml
Created July 18, 2011 23:22
Resulting xml elements returned by XQuery
<Document id="3" time="6/9/2010"/>
<Document id="4" time="8/10/2010"/>
cd d:\var\projects
for $doc in //Document
let $datestr := $doc/@time
let $datearr := tokenize($datestr, '/')
let $year := $datearr[3]
let $month := format-number(xs:int($datearr[2]), '#00')
let $day := format-number(xs:int($datearr[1]), '#00')
let $dateisostr := concat($year, '-', $month, '-', $day)
where $dateisostr > '2010-06-01' and $dateisostr < '2011-01-01'
(:
let $date := xs:date($dateisostr)
@vlcinsky
vlcinsky / my.xml
Created July 20, 2011 14:07
some xml
<Documents>
<Document id="1" time="5/2/2010">
</Document>
<Document id="2" time="4/8/2011">
</Document>
<Document id="3" time="6/9/2010">
</Document>
<Document id="4" time="8/10/2010">
</Document>
</Documents>
@vlcinsky
vlcinsky / bucketlistresultset.py
Created September 18, 2011 21:13
Modified boto to overcome shortened listing of object versions in AWS S3 problem
def versioned_bucket_lister(bucket, prefix='', delimiter='',
key_marker='', version_id_marker='', headers=None):
"""
A generator function for listing versions in a bucket.
"""
more_results = True
k = None
while more_results:
rs = bucket.get_all_versions(prefix=prefix, key_marker=key_marker,
version_id_marker=version_id_marker,
@vlcinsky
vlcinsky / age_in_minutes.py
Created October 4, 2011 12:21
calculate_age_in_minutes explained
def calculate_age_in_minutes(rec_time, pub_time):
"""
pub_time .. string with datetime in ISO 8601 format. Time, when is the feed published (mostly current time).
rec_time .. string with datetime in ISO 8601 format. Time, when record was created.
sample ISO string: 2011-10-25T13:55:42.123Z
return .. integer with duration between pub_time and rec_time expressed in minutes
"""
#parsing pub_time to datetime structure. Ignoring timezone and fractions of seconds
pub_t = datetime.datetime.strptime(pub_time[:19],"%Y-%m-%dT%H:%M:%S")
@vlcinsky
vlcinsky / placfun.py
Created March 18, 2014 23:09
Create annotated function preserving original function intact
""" A study, how to create plac annotated command line script from bare function
preserving original function intact.
This is sometime needed, when the same function is used for call e.g. by a celery,
and for manual testing it is handy to have it in command line version.
Usually, the code (producing only annotated command line tool) looks like::
import plac
@vlcinsky
vlcinsky / csv2sql.py
Created April 29, 2014 05:53
Convert CSV file into SQL script
"""
Usage:
csv2sql.py [--table <tablename>] <csvfile>
Options:
--table <tablename> Name of table in database to import into [default: mytable]
Convert csv file with iperf data into sql script for importing
those data into MySQL database.
"""