Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / snippets.py
Created July 4, 2012 15:34
Python snippets
#!/usr/bin/env python
if name == 'Foo':
foo()
elif name == "Bar':
bar()
else:
wtf()
numbers = ['one', 'two', 'three']
@kristopherjohnson
kristopherjohnson / entityframeworksnippets.cs
Created July 11, 2012 14:38
Entity Framework snippets
using (MyEntities context = new MyEntities())
{
// Query a collection
IQueryable<MyThing> thingsQuery = from thing in context.Things
where thing.Name == "Foo"
select product;
foreach (var t in thingsQuery)
{
DoSomethingWith(t);
}
@kristopherjohnson
kristopherjohnson / HttpTextRequestTask.java
Created July 26, 2012 14:45
Asynchronous retrieval of text via HTTP for Android
package blah.blah.blah;
import android.os.AsyncTask;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
@kristopherjohnson
kristopherjohnson / JSONHttpPost.java
Created July 27, 2012 17:59
Create a JSON POST request for Android
/**
* Create HttpPost request with a JSON string body, expecting a JSON response
*/
private HttpPost createJSONPostRequest(String emailAddress, String data) throws JSONException, UnsupportedEncodingException
{
JSONObject json = new JSONObject();
json.put("protocolVersion", "1.0");
json.put("emailAddress", emailAddress);
json.put("data", data);
String jsonString = json.toString();
@kristopherjohnson
kristopherjohnson / FtpDownloadFileText.cs
Created July 31, 2012 19:48
C# snippet: Download a file via FTP, returning response as a String
static String FtpDownloadFileText(String fileURL, String userName, String password)
{
var request = WebRequest.Create(fileURL);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(userName, password);
var response = (FtpWebResponse) request.GetResponse();
var responseStream = response.GetResponseStream();
var reader = new StreamReader(responseStream);
@kristopherjohnson
kristopherjohnson / XmlDeserializationExample.cs
Created August 1, 2012 14:48
C# snippet: Example of deserializing XML
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
public sealed class ServerConfig
{
public sealed class Server
{
[XmlAttribute("host")]
public string Host { get; set; }
@kristopherjohnson
kristopherjohnson / ReadResourceFile.cs
Created August 1, 2012 17:53
C# snippet: Read embedded resource file
/// <summary>
/// Read contents of an embedded resource file
/// </summary>
private string ReadResourceFile(string filename)
{
var thisAssembly = Assembly.GetExecutingAssembly();
using (var stream = thisAssembly.GetManifestResourceStream(filename))
{
using (var reader = new StreamReader(stream))
{
@kristopherjohnson
kristopherjohnson / gravatar.sh
Created August 13, 2012 20:21
Download Gravatar image for an email address
curl "http://gravatar.com/avatar/$(md5 -q -s myemail@example.com)" > avatar.png
@kristopherjohnson
kristopherjohnson / GetXmlElementValue.cs
Created September 12, 2012 14:34
Extract value of an XML element from a document
/// <summary>
/// Extract an element value from an XML document
/// </summary>
/// <param name="xmlDocumentString">XML document string</param>
/// <param name="xmlns">XML namespace</param>
/// <param name="elementName">name of element whose value is to be returned</param>
/// <returns>System.String</returns>
/// <exception cref="System.Exception">thrown on failure</exception>
public static string GetXmlElementValue(string xmlDocumentString, string xmlns, string elementName)
{
@kristopherjohnson
kristopherjohnson / CreateDataSetFromXMLString.cs
Created September 12, 2012 15:26
Load a DataSet from an XML string (.NET)
public static DataSet CreateDataSetFromXMLString(string xml)
{
var dataset = new DataSet();
using (var sr = new StringReader(xml))
{
dataset.ReadXml(sr);
}
return dataset;
}