Skip to content

Instantly share code, notes, and snippets.

@yyl
yyl / builtin.py
Created July 13, 2012 16:44
Iters
L = [1,2,3,4,5]
D = {'a':1,'b':2,'c':3}
i = iter(L)
print i
print D.iterkeys()
print D.itervalues()
print D.iteritems()
for elt in i:
print elt
i.next()
@yyl
yyl / gist:3095164
Created July 12, 2012 01:56
Notification
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, TrackingActivity.class), 0);
builder = new NotificationCompat.Builder(getBaseContext())
.setContentTitle("You are being tracked...")
@yyl
yyl / alert.java
Created July 11, 2012 21:12
Toggle and Alert Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Choose mode for tracking")
.setCancelable(false)
.setPositiveButton("Only GPS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LocationData.this.doPositiveClick();
}
})
.setNegativeButton("All providers", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
@yyl
yyl / receiver.java
Created July 11, 2012 16:51
local broadcast manager usage
// register the receiver you created and
// add a filter to only receive the intent you need
LocalBroadcastManager.getInstance(this).registerReceiver(
myReceiver, new IntentFilter("location"));
// create your own receiver from BroadcastReceiver class
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// get the info from the intent we received
@yyl
yyl / datetime.sh
Created July 7, 2012 20:42
datetime module in python
>>> datetime.now()
datetime.datetime(2012, 7, 7, 16, 31, 24, 604514)
>>> datetime.today()
datetime.datetime(2012, 7, 7, 16, 31, 27, 340110)
>>> datetime.utcnow()
datetime.datetime(2012, 7, 7, 20, 31, 58, 227201)
>>> d = datetime.strptime("2012-07-07 16:00", "%Y-%m-%d %H:%M")
>>> d
datetime.datetime(2012, 7, 7, 16, 0)
>>> datetime.utcnow().timetuple()
@yyl
yyl / creattable.sql
Created July 6, 2012 19:15
learning postgresql
CREATE TABLE cities (
city varchar(80) primary key,
location point
);
CREATE TABLE weather (
city varchar(80) references cities(city),
temp_lo int,
temp_hi int,
prcp real,
@yyl
yyl / login.html
Created June 28, 2012 17:47
django built-in login demo
{% extends "base.html" %}
{% load url from future %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
@yyl
yyl / gist:3011271
Created June 28, 2012 13:08
Euler project 12 triangle number
from math import sqrt, floor
def euler12():
D = 0
n = 1
step = 2
while D < 500:
n += step
step += 1
D = findDivisor(n)
@yyl
yyl / gist:2991058
Created June 25, 2012 20:36
List model for project grehelper
class List(models.Model):
name = models.CharField(max_length=200, unique=True)
start_date = models.DateTimeField(verbose_name='date started',
default=datetime.now())
slug = models.SlugField(max_length=200, unique=True)
progress = models.FloatField(default=0.0,
validators=[MaxValueValidator(100.0),
MinValueValidator(0.0)])
def __unicode__(self):
@yyl
yyl / gist:2983669
Created June 24, 2012 15:23
host file path
Windows/system32/drivers/etc/host
/private/etc/hosts