Skip to content

Commit abced43

Browse files
2 parents caa21a5 + 8df7bc6 commit abced43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+6592
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
class FibIterator(object):
4+
5+
def __init__(self, count):
6+
self.values = [0,1]
7+
self.i = 0
8+
self.count = count
9+
10+
def __iter__(self):
11+
return self
12+
13+
def next(self):
14+
if self.i >= self.count:
15+
raise StopIteration
16+
17+
self.i += 1
18+
new_value = sum(self.values)
19+
self.values.append(new_value)
20+
return self.values.pop(0)
21+
22+
import sys
23+
24+
iter = FibIterator(int(sys.argv[1]))
25+
for i,x in enumerate(iter):
26+
print "%d: %d" % (i+1,x)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import itertools
2+
3+
items = itertools.chain(xrange(10), FibIterator(10))
4+
# sort the items for groupby:
5+
sorted_items = sorted(items)
6+
grouped_items = itertools.groupby(sorted_items)
7+
for key, it in grouped_items:
8+
print "%s: " % key
9+
for x in it:
10+
print "\t%s " % x
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import operator
2+
3+
f = [x.strip() for x in open("/usr/share/dict/words").readlines() if len(x) > 3]
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
f.sort(key=operator.itemgetter(1,0), reverse=True)
30+
print f[0:2]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
3+
import bz2
4+
import xml.etree.ElementTree as ET
5+
6+
7+
if __name__ == "__main__":
8+
fname = "data/enwiki-latest-pages-articles1.xml-p000000010p000010000-shortened.bz2"
9+
10+
f = bz2.BZ2File(fname)
11+
12+
tree = ET.parse(f)
13+
root = tree.getroot()
14+
15+
namespaces = {'xmlns': 'http://www.mediawiki.org/xml/export-0.8/'}
16+
for title in root.findall('xmlns:page/xmlns:revision/xmlns:contributor/xmlns:username', namespaces=namespaces):
17+
# for title in root.findall('{http://www.mediawiki.org/xml/export-0.8/}page/{http://www.mediawiki.org/xml/export-0.8/}title'):
18+
print title.text
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import bz2
4+
from xml.dom import minidom
5+
6+
if __name__ == "__main__":
7+
fname = "data/enwiki-latest-pages-articles1.xml-p000000010p000010000-shortened.bz2"
8+
9+
f = bz2.BZ2File(fname)
10+
11+
doc = minidom.parse(f)
12+
13+
for element in doc.getElementsByTagName("title"):
14+
15+
text_node = element.childNodes[0]
16+
17+
if text_node.data.startswith("Android"):
18+
parent = element.parentNode
19+
clone = element.cloneNode(True)
20+
parent.appendChild(clone)
21+
22+
print doc.toprettyxml(encoding="utf-8")
23+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
import bz2
4+
from xml.dom import minidom
5+
6+
if __name__ == "__main__":
7+
fname = "data/enwiki-latest-pages-articles1.xml-p000000010p000010000-shortened.bz2"
8+
9+
f = bz2.BZ2File(fname)
10+
11+
doc = minidom.parse(f)
12+
13+
element = doc.getElementsByTagName("page")[-1]
14+
15+
el = doc.createElement('modifiedby')
16+
txt = doc.createTextNode('joseph')
17+
el.appendChild(txt)
18+
19+
parent = element.parentNode
20+
clone = element.cloneNode(True)
21+
clone.appendChild(el)
22+
parent.appendChild(clone)
23+
24+
print doc.toprettyxml(encoding="utf-8")
25+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
3+
import bz2
4+
import logging
5+
from xml import sax
6+
7+
logging.basicConfig(level=logging.INFO)
8+
logger = logging
9+
10+
class WikiHandler(sax.handler.ContentHandler):
11+
IN_TITLE = False
12+
IN_USERNAME = False
13+
SEEN = dict()
14+
15+
def startElement(self, name, attrs):
16+
if name == "title":
17+
logger.debug("in title")
18+
self.IN_TITLE = True
19+
if name == "username":
20+
logger.debug("in username")
21+
self.IN_USERNAME = True
22+
23+
def characters(self, content):
24+
if self.IN_TITLE:
25+
logger.info(content)
26+
27+
if self.IN_USERNAME:
28+
if self.SEEN.has_key(content):
29+
self.SEEN[content] += 1
30+
else:
31+
self.SEEN[content] = 1
32+
33+
def endDocument(self):
34+
for k,v in self.SEEN.iteritems():
35+
print "%s: %d" % (k,v)
36+
37+
def endElement(self, name):
38+
if name == "title":
39+
logger.debug("out title")
40+
self.IN_TITLE = False
41+
if name == "username":
42+
logger.debug("out title")
43+
self.IN_USERNAME = False
44+
45+
if __name__ == "__main__":
46+
# fname = "data/enwiki-latest-pages-articles1.xml-p000000010p000010000.bz2"
47+
fname = "data/enwiki-latest-pages-articles1.xml-p000000010p000010000-shortened.bz2"
48+
49+
f = bz2.BZ2File(fname)
50+
51+
handler = WikiHandler()
52+
53+
sax.parse(f, handler)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import datetime
2+
import itertools
3+
import sqlite3
4+
5+
import dateutil.parser
6+
import pytz
7+
8+
"""Dates and times in sqlite3 are stored as TEXT, REAL, or INTEGER values
9+
10+
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
11+
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
12+
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
13+
14+
"""
15+
16+
def adapt_datetime(dt):
17+
return dt.isoformat().replace('+','-')
18+
def convert_datetime(ts):
19+
return dateutil.parser.parse(ts)
20+
21+
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
22+
sqlite3.register_converter('timestamp', convert_datetime)
23+
24+
input_values = []
25+
26+
input_values.append([datetime.datetime(2019,11,1,12,0, tzinfo=pytz.UTC)])
27+
28+
us_pacific_tz = pytz.timezone('US/Pacific')
29+
input_values.append([datetime.datetime(2019,11,1, tzinfo=us_pacific_tz)])
30+
31+
conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
32+
33+
cursor = conn.cursor()
34+
35+
cursor.execute("CREATE TABLE timetable(t timestamp)")
36+
37+
cursor.executemany("INSERT INTO timetable(t) VALUES (?)", input_values)
38+
39+
for row,input_value in itertools.izip(cursor.execute("SELECT t FROM timetable"), input_values):
40+
from_db = row[0]
41+
from_input = input_value[0]
42+
print from_db
43+
print type(from_db)
44+
print from_input
45+
assert(from_db == from_input)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import datetime
2+
import itertools
3+
import sqlite3
4+
5+
import pytz
6+
7+
"""Dates and times in sqlite3 are stored as TEXT, REAL, or INTEGER values
8+
9+
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
10+
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
11+
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
12+
13+
sqlite3 will handle the translation from datetime to strings
14+
"""
15+
16+
input_values = []
17+
18+
input_values.append([datetime.datetime(2019,11,1,12,0)])
19+
input_values.append([datetime.datetime(2019,11,1,13,0)])
20+
# input_values.append([datetime.datetime(2019,11,1,12,0,tzinfo=pytz.UTC)])
21+
22+
conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
23+
24+
cursor = conn.cursor()
25+
26+
cursor.execute("CREATE TABLE timetable(t timestamp)")
27+
28+
cursor.executemany("INSERT INTO timetable(t) VALUES (?)", input_values)
29+
30+
for row,input_value in itertools.izip(cursor.execute("SELECT t FROM timetable"), input_values):
31+
from_db = row[0]
32+
from_input = input_value[0]
33+
print from_db
34+
print type(from_db)
35+
print from_input
36+
assert(from_db == from_input)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
## v1.1.0
4+
5+
- Expand `beforeInit` event to allow halting of init event.
6+
- Create alternative init signature with single options object using new `options.selectors.slides` option.
7+
- Added methods `getTopLevelSlides` and `getNestedSlides`.
8+
- Integrated hash plugin into core.
9+
- Allow for touch swiping to be axis specific or disabled.
10+
- Include ARIA attribute considerations in core and extensions.

0 commit comments

Comments
 (0)