I discovered that you can use hilite.me to format source code so it looks nice on blogger.
Paste the source into hilite.me. It gives you pretty html that you can paste into an html editor
print("Hello World");
This blog helps me keep track of programming tricks I've learned, so I don't forget them later
I discovered that you can use hilite.me to format source code so it looks nice on blogger.
Paste the source into hilite.me. It gives you pretty html that you can paste into an html editor
print("Hello World");
I'm writing a library file for django, and I want it to store settings in the settings.py file, which is django's default configuration location for settings. However, I still want the library to work if django isn't installed. Here's how I did it :
This way, it doesn't crash if the reference doesn't exist
try: from django.conf import settings except ImportError: pass
Research online said python doesn't have a direct way to check if the reference exists, so you can use a try / except block instead
try: settings except NameError: settings = None
If settings exists, use the django settings.py file. Otherwise use a flat file
if settings != None: if not settings.configured: settings.configure() self._key_list = settings.MY_LIB_SETTINGS else: filename = "lib_api_config.txt" f = open(filename,"r") file_contents = f.read() f.close() self._key_list = json.loads(file_contents)
The edit api page has a sample html / javascript code that you can put on your server to display a "Hello World" type application that displays a map.
I had some issues, which I've noted above
<table id="tbData">You set up a template like this :
<tr><td>Name</td><td>Phone</td></tr>
<div id="phoneList">
</div>
</table>
<script id="phoneTmp" type="text/x-jquery-tmpl">If you do this, your header will show up at the bottom of the table in Firefox, but Internet Explorer will hate you. Or you will hate Internet Explorer. One of those. I get confused. The solution : use tbody tags instead of div tags when you're inserting, showing, or hiding rows in a table with jquery. This forces me to break my longstanding policy of not using any html tags developed after 2000, but it's saved me a lot of headaches.
<tr><td>${firstname},${lastname}</td><td>${address}</td><td>${phone}</td></tr>
</script>
<table id="tbData">Click "Result", or the little "play" arrow to see what this code does
<tr><td>Name</td><td>Phone</td></tr>
<tbody id="phoneList">
</tbody>
</table>
var patt=/pattern/modifiers;In my case, I wanted to see if my string contained anything that wasn't a number, period, or dollar sign. The following code does the trick
function isNumeric(str) {The regular expression [^0-9$.] searches the string for any character that isn't a number (0-9), a dollar sign, or a period. Alpha.test returns true if any "bad" characters are found. If alpha.test is false, then we have a valid number
var alpha = /[^0-9$.]/g
return !alpha.test(str);
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>Next, I embed a JSON string on my page. (This is javascript, so it goes in javascript tags).
<script src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
var strData = '[{"firstname":"Dave","lastname":"Stillman","phone":"555-1212","address":"555 1212 Road, Nampa, ID"},{"firstname":"Donald","lastname":"Trump","phone":"555-5512","address":"255 Somewhere Lane, New York NY"}]';I use eval to turn my json string into a javascript object.
var data = eval("(" + strData + ")");Next, I create a template script. I create it the same way I would include inline javascript in a page, but I give it type='text/x-jquery-tmpl' instead of type='javascript'. I also give it a unique ID, so I can access it later.
<script id="addressTmp" type="text/x-jquery-tmpl">Next, I add a div where I want the data to be displayed
<b>${firstname},${lastname}</b><br>
${address}<br>
${phone}<br>
</script>
<div ID="addressDiv" />Finally, I use a jquery template command to display the data
$("#addressTmp").tmpl(data).appendTo("#addressDiv");Links