Learning Python

Posted by Chief on Apr 23, 2010 in Lessons, Synopses
No Comments

I dove into Python yesterday. Today: I’m reading the tutorials. I like it. What did you expect? Everyone seems to like it, right? Have you met a person who learned Python that hates the language?

So, I’m still n00b status, but here’s the deal: It’s got all the things I love about Javascript, the good parts of Java (no bloat), and the fundamental simplicity of C. PHP is conspicuously absent here, but … now that I mention it, there’s something there too.

Let’s over go over those, since you may be flaming me already. Hey! I’m still a n00b.

Javascript I really like the function mapping aspects. Ok, so this is more of a jQuery-type thing. But it’s possible, and jQuery makes it obvious — in-your-face — how useful this feature is. I could care less about lambda functions right now, but I think they’re related. I like the way zip() works on arrays.

Java Ah, the language of verbosity. I’d rather dictate War and Peace, than write Java. What’s so good about Java? It’s got a great package system, and the try/catch/finally construct. Python’s equivalent: try/except/finally. Thumbs up! Java has a good class hierarchy that Python seems to duplicate (minus the Java overhead).

C There’s nothing more powerful than C. Assembly’s got nuthin’ on C. C is the ultimate speed-speed trade-off. The speed of code development vs the speed of execution. Everything else is derivative bloat that hinders performance. So, how does Python act like C? Well, the BDFL gives Python a coherent structure. The Java package sprawl problem is not present. Code bases are tight. Syntax is rigid. Writing Python is like clock-work — it must be precise.

PHP Don’t flame. Here it is. Python is extra-great because it’s interpreted. PHP is interpreted. That is all.

Confusion says: The n00b walks a fine line of asking and testing. Nobody ever said learning a language was easy. It should be fun (or interesting if you don’t like learning) though! So here’s the deal: I’m not flying yet. I’m tutorializing. Short programs are useful learning tools, but jumping in the deep end really makes you sink or swim — strangely, I’m trying to be trite right now. I don’t get lambda. I don’t know why there are so many different types of lists. I know I’ve had a need for sets, lists, hashes, tuples, queues, and the like, but is it really necessary that there be a specific data type for each? Whatever, I’m a n00b. I know performance is probably the real motivator, but c’mon! It’s interpretive. I was surprised to learn that compiled Python (.pyc) does not execute any faster than inline Python (.py) (Can’t find the reference). The .pyc files make the module load faster, not execute faster.

Anyway, right now, Python makes a great calculator. Anyone living in Hawai’i will tell you the numerous “spam and eggs” references make’s ‘em hungry too!

Tags: , , , , , ,

Git: Recovering a file that you deleted

Posted by Chief on Mar 10, 2010 in Lessons, Reference, Scripts, Tricks and Hacks
No Comments

You might be doing some spring cleaning to your source code, or you might move files around that you think are unnecessary. Later on, you realize that one of the files you removed was a dependency. Now what? For this, we use git checkout.

If this is you:

...edit files...
git add edited-file
git commit -m "made changed"
git rm seemingly-useless-file
git commit -m "removed unreferenced dependency"
... edit file ... realize you dynamically included that file elsewhere..

Then you can simply follow up with:

git checkout 0a323 // the previous revision (hash from `git log`)
cp seemingly-useless-file seemingly-useless-file.1
git checkout master
mv seemingly-useless-file.1 seemingly-useless-file
git add seemingly-useless-file
git commit -m "Restored seemingly-useless-file"

You may want to git blame yourself while you’re at it.

If you know of a better way, please let me know.

Tags: , , , , , , , , ,

Google Earth Icons with Heading

Posted by Chief on Jan 19, 2010 in Lessons
No Comments

I’ve discovered that icons given headings in Google Earth KML stopped orienting in the specified heading direction. The result is that when a user rotates the Earth so that true North is not directly “up” on the screen, the angle of the icon is misleading. For wind barbs, this could lead to dangerous decisions being made if the viewport isn’t manually corrected to set up = true north.

There are several known branches of this problem.

One involves the Google Earth browser plug-in:

Apparently, this incorrect handling is the end result of a bug fix to “correct” icon heading behavior.
Google Earth Plug-in – 5.1.3506.3999

(Issue 131) Icon headings should now behave as expected, and consistent with the Google Earth desktop client. http://www.noeman.org/gsm/mac-other-oses-softwares/106515-google-earth-google-earth-plug.html

More on this bug (affecting version 5.1.x): http://www.google.com/support/forum/p/earth/thread?tid=005bea9c26949e40&hl=en

Work-arounds: GroundOverlay and use a Colada model, both ugly.

Another instance of the icon heading bug:
There is another situation in which the heading of the icon is not obeyed. This occurs when an invalid styleUrl is used in the KML. One recommendation is to remove the “#” character. ex: <styleUrl>#balloonStyle</styleUrl> is no longer correct as of version 5x. I have not confirmed this, but I’ve heard that this is correctly implemented as <styleUrl>balloonStyle</styleUrl>. What happend? This isn’t very backwards-compatible nor user-friendly. Why the change? HTML hashes are logically sound, as they refer to a locally named entity, such as those specified by a style id.

What worked for me was to completely remove the styleUrl when providing a local Style. See below.

Broken:

1
2
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
 <name>Weather Stations</name>
 <description><![CDATA[<p>Generated: 2010-01-19 22:03:45 UTC</p> <p>Only stations reporting <b>within 3 hours</b> are included in this document.</p>]]></description>
 <open>1</open>
 <Style>
  <ListStyle>
   <listItemType>radioFolder</listItemType>
   <bgColor>00ffffff</bgColor>
   <maxSnippetLines>2</maxSnippetLines>
  </ListStyle>
 </Style>
 <Style id="balloonStyle">
  <BalloonStyle>
   <text><![CDATA[
   <h2 style="font-size:1.1em;border-bottom:solid #333 1px;">Instrument:
   $[name]</h2>
   $[description]
   ]]></text>
  </BalloonStyle>
 </Style>
 <Folder>
  <name>Visibility and Avg Winds</name>
  <Style>
   <ListStyle>
   <listItemType>checkHideChildren</listItemType>
   <bgColor>00ffffff</bgColor>
   <maxSnippetLines>2</maxSnippetLines>
  </ListStyle>
 </Style>
 <visibility>0</visibility>
 <Placemark>
  <name>Any Instrument ID</name>
  <description><![CDATA[ <p><font color="#999">Sample Date</font></p> <div>Data Table Here</div>]]></description>
  <styleUrl>#balloonStyle</styleUrl>
  <Style>
   <IconStyle>
    <scale>3.25</scale>
    <heading>0</heading>
    <Icon>
     <href>http://www.example.com/barb.php?spd=4&amp;dir=74&amp;val=7&amp;col=65280&amp;dia=100</href>
    </Icon>
   </IconStyle>
  </Style>
  <Point>
   <coordinates>-117.1135,32.3325,0</coordinates>
  </Point>
 </Placemark>
 </Folder>
</Document>
</kml>

Working:

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 <Placemark>
  <name>Any Instrument ID</name>
  <description><![CDATA[ <p><font color="#999">Sample Date</font></p> <div>Data Table Here</div>]]></description>
  <Style>
   <IconStyle>
    <scale>3.25</scale>
    <heading>0</heading>
    <Icon>
     <href>http://www.example.com/barb.php?spd=4&amp;dir=74&amp;val=7&amp;col=65280&amp;dia=100</href>
    </Icon>
   </IconStyle>
  </Style>
  <Point>
   <coordinates>-117.1135,32.3325,0</coordinates>
  </Point>
 </Placemark>

Tags: , , , , ,

On the Millennials and Having Children

Posted by Chief on Jan 12, 2010 in Lessons, Quotes
No Comments

Everyone wants a baby, but no one wants a kid these days. Runaway1956

Kids today get emo and suicidal because they have been given everything, never had to earn anything, never been hungry, never had anything real to fear, never been punished for their behavior and are bored with having too much entertainment. Nadaka

Tags: , , , , , , , , ,

Burning Rope for 45 Minutes

Posted by Chief on Nov 23, 2009 in Lessons
1 Comment

Premise: There are two ropes of equal length and physical composition. Each rope takes one hour to burn from end-to-end, but may burn at a variable rate. You are given a lighter and these two ropes. There is nothing else that can be utilized, not sand nor clock nor sun.

Question: How can you determine when 45 minutes have elapsed?

Answer: Light one rope at both ends. Light the other rope at only one end. When the rope that was burning from both ends has burnt out, 30 minutes have passed. Light the second end of the rope that was only burning from one end. This rope will burn out in 15 more minutes. The total time will have been 45 minutes.

Tags: , ,

Things that Kill MySQL Performance

Posted by Chief on Nov 4, 2009 in Lessons
No Comments
  1. Failing to plan for scaling out, scaling up
  2. Not using EXPLAIN (learn from your mistakes)
  3. Using the wrong data types (hint: use smallest fixed-size)
  4. Using persistent connections in PHP (leads to zombie processes)
  5. Using a heavy DB abstraction layer (hint: use PDO or custom)
  6. Using the wrong storage engine (MEMORY, ARCHIVE, InnoDB, etc.)
  7. Using indexes improperly (hint: select in the order of the index; hint: keep primary key small)
  8. Issuing SQL queries that can’t be cached easily; having too large of a query cache
  9. Using stored procedures when prepared statements are better
  10. Using functions on indexed columns in the WHERE clause
  11. Not indexing important columns; having redundant indexes
  12. Using sub-queries when joins would be better
  13. Issuing avoidable deep scans
  14. Doing SELECT COUNT(*) without WHERE on InnoDB table.
  15. Failing to profile/benchmark your SQL
  16. Not using AUTO_INCREMENT if applicable
  17. Not using ON DUPLICATE KEY UPDATE (1 query vs 2)

Reference:

Tags: , , , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.