Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

September 9, 2021

How to Read Advanced Neural Networks in 30 Seconds

We, Candopi Team 1, are showing off.

Just take a look inside the folder titled "911_write_neural_networks_in_30_seconds."

We are showing, the Harvards, the Stanfords, the MITs, the Yales, the Princetons, the Oxfords, and the Cambridges, how to teach coding/ programming/ hacking to the masses.
Not merely to high priests in the ivory towers.
It's the first bite of the apple in the garden of Eden.









September 3, 2021

Candopi Programming 101 and The Luck Factor

I once read "The Luck Factor" by the psychologist Richard Wiseman.
Now I read a neat summary of the book in "The Smarter Startup: A Better Approach to Online Business for Entrepreneurs" by Neal Cabage and Sonya Zhang.
Very neat. How can you be luckier?
  1. Alert, and open to opportunities
  2. Decide by intuition
  3. Manifest
  4. Resilience and gratitude
Each point is deep. I'll elaborate only on the most laughable point, Point 3. If I can defend the most laughable, I can defend others way better.

"Manifest?" "The Secret?" "The Law of Attraction?" Shamans? Roll your eyes, okay.

But what about "Shaping" strategy that they mention a lot in Harvard Business Review, Jack Welch, Sun Tzu, Von Moltke (Helmuth von Moltke the Elder), the US Marines manual?
What about "Effectuation" (Sarasvathy), too?

Candopi Programming 101 teaches you luck.




We, Candopi Team 1, are showing off.

We are showing, the Harvard's, the Stanford's, the MITs, the Yale's, the Princeton's, the Oxfords, and the Cambridge's, how to teach coding/programming/hacking to the masses.
Not merely to high priests in the ivory towers.
It's the first bite of the apple in the garden of Eden.



It's Prometheus bringing fire to mankind.




Can Programming Raise Your IQ?

Efforts that don't raise IQ

First, Recent research suggests that taking IQ training does raise your IQ test score, but not your IQ!
What a wicked finding?

Some Indian educators also admit that entrance exams to IIT are more practice-based than true tests of student potential.
Translation: The more you practice, the higher your score, but your true skills in general may not rise that much.
Second, Feynman also made a similar remark about learning physics and making yourself smarter.

Efforts that do raise IQ

First, Drucker's remark about Jesuit learning (in "Innovation and Entrepreneurship," I think.)
Second, What Larry Bossidy and Ram Charan talk about executive self-development in "Execution: The Discipline of Getting Things Done."

They say in effect: Once an executive can resolve/dissolve "blocks" to his/her own development, there is no limit to their potential.

Very awe-inspiring passages. I do recommend you find read them.

Third, Self-reflection, awareness, and mindfulness in general. Because such practices make you aware of your own mistakes, what others have done better than you, and so on. It is a lot like model Deep Learning neural networks. You measure your own results, adjust/update your own "weights" till you reduce your error to zero.
Candopi Programming 101 makes you alert, aware and mindful.
If math, the so-called mother of science, is about patterns, we are about seeing, surfacing, uncovering such patterns.




September 2, 2021

Candopi Teams

 Our business is made up of 3 teams. We're having lots of fun.


Team 3: Genomic data science: To identify and modify cancers and viruses.

Team 2: AI-driven Ad tech and Mar tech: A tight focus on product marketing.

Team 1: Teaching programming, software engineering, and computer science. 
Candopi Programming 101 at GitHub















September 1, 2021

Candopi Programming 101 : How to Learn Algorithms

 Candopi Programming101 

HOW TO LEARN ALGORITHMS


PART 1: Selection

1. You select by reputation, looks and feels.

2.You select by running a given code. These days you get about 3000-plus new math research papers every week.
  In biology/medicine field, according Yancopolous(of Rengenron), 60 to 90 % research reports are either irrelevant or wrong! 

PART 2: Learning

These great scientists disagree:
1. I work on/with the paper : Feynman.
2. Pencil-paper simulation is the most painless method to learn algorithms: Donald Knuth

3. No scientist thinks in equations: Einstein.
4. After paralysis, Stephen Hawking worked even better without pencil and paper.: An American contemporary physicist with Hawking
5. With certain types of problems, once a student picks up paper to solve, he loses the ability to solve it : Edgar Dijkstra.

Both groups are right. 

(1) You must run the code on paper. 
(2) But at certain spots, that will get you into trouble (just as Dijkstra warned.) 
Then, you run it in your mind, especially the conditions, corner cases, edge cases. 


REMEMBER the whole field is small. Technology moves super-slowly, as Steve Jobs kept saying.
So if you cannot learn an algorithm today, it means nothing.

Learning it today or learning it 3 months later  or 3 years later, makes ZERO difference.
These algorithms are invented or discovered 1 in every decade. You have time. 


Candopi Programming 101 : What's in it for you?

Candopi Programming101 

BENEFITS:

You can become a software billionaire, a great data scientist or anything in between.

You become ready to explore more, master more.

  1. As you can see in my own examples (Java, C++, Python, Ruby in the folders : /7_data_structures_and_algorithms/sorts_python and /7_data_structures_and_algorithms/quick_sort_python), JavaScript you learn here is very close to any other language: Java, C++, Python, PHP, Ruby or any other.
  2. Moving to Web development, Mobile development, app development, blockchain development etc, too is smooth and easy.
  3. Moving to AI, Machine Learning, Data Science etc, too is smooth and easy. (But you need a bit of math, at least A-level, or US high school level.) e.g. Neural networks, Convolutional networks (CNN), RNN, Capsule networks, One-Shot, Few-Shots etc are also made up of "while" and "for" loops you learn here.

COSTS:

  1. The course is very easy, even for primary school students.
  2. You don't need any experience.
  3. You do zero installation.
  4. The course takes just 20 to 40 hours. And zero fees.


IF YOU GET INTO TROUBLE:

  1. Say this mantra 3 times: "I'm alright. This person cannot teach. I will find a better teacher, a better book."
    As the great George Polya says, emotions is a big part of math teaching. Or any STEM teaching in general. Blame us. Don't blame yourself. Do you think we are Gods who can never err?
  2. Better still, tell us. We can, we will change this a million times for our students' sake. Try us.



January 28, 2013

Simplicity Despite of Technical Knowledge

Old Style Javascript


A web page is a simple concept to understand, and that is why it caught on so fast in the first place.

And our desires to interact with it are also simple.

Say, there are a number of thingies ("a", "b", "c", "d", and "e") on the page and I want to take some simple actions on them e.g. ( "touch", "drag", "mouse-enter", "mouse-move-away", "click", "focus" and so on ).

Thus, if I click "a" on the web page, I expect some reaction.
Thus, if I drag "b" on the web page, I expect some reaction, maybe the same or something different.

Very simple and very reasonable expectations.
But what did old javascript offer?

Your action: Click a submit button of a form
Your expected reaction: Check the value of a field/blank in the form

Then, you must code/write something long, verbose and ugly. You can verify this by reading any old javascript book.

My Style Javascript


I refuse to touch such smelly, messy stuffs. So, I did something like:


onClick="clicker('id1964')"     

onMouseOver="mover('id1')" 

onMouseOut="mouter('id1964')"  

onClick="displayText('id11', 'Khine 1980');"


/* KYAW TEST June 25, 2007 */

function loader() { alert("on load on body tag"); }

function unloader() { alert("on unload on body tag"); }

function blure() { alert("on blur on body tag"); }

function focuse() { alert("on focus on body tag"); }

function clicker(elemId) {
    var elem=document.getElementById(elemId)
    elem.style.color="red"
    alert("this box follow the link anyway!!!");  
}
function mover(textAreaName) {
    document.getElementById(textAreaName).style.visibility="hidden";
    alert("blah");  
}
function mouter(textAreaName) {
    document.getElementById(textAreaName).style.visibility="visible";    
}

function displayText(textId, mess) {    
    document.getElementById(textId).firstChild.nodeValue=mess; 
}

The function "loader" acts when something is loaded.
The function "focuse" acts when something is focused.
The function "mouter" acts when the mouse is outted.
And so on.

Does it take brains to write simply like that?

Let jQuery Beat Me But ...


As you can see in the comment lines, these were done in June 2007. At that time, I wasn't keen on javascript and so hadn't heard of jQuery and John Resig and his wonderful inventions and re-inventions.  Now with his marvellous library, to get similar effects, I write just 1 or 2 lines.

Conclusion


Insist on simple, reasonable stuffs as a discerning buyer, on behalf of your own customers.
Refuse to use, reuse messy codes.


If you can find a John Resig, use his/her stuffs.

If you can't, at least make and keep things simple for yourself and, above all else, for your customers.

January 16, 2013

Polyglot Programming, Toolbox Programming

This is , in part, in response to the comments for our Ruby Tutor's Ruby Meta-programming series. Many commentators have suggested that we also try out their favorite programming languages such as Lisp, F#, Scala, Clojure etc.

Polyglot Programming


When you are in Munich, speak German.
When you are in Paris, speak French.
When you are in Osaka, speak Japanese.

All very fine. We have no qualms about switching languages provided we have fluency in those languages.

Does the parallel hold in software?

Write PL/SQL here, code in SQL-92 here, use hibernate here, employ iBatis here.
Use Java there, code in C here, write PHP there, write Ruby here.
Does the parallel really hold in software? I don't think so and here are my reasons.

  1. Our attachments to our chosen programming language is far stronger than our attachments to our mother tongues. We switch our human languages depending on who we are talking to. We do so without second thoughts, without hesitation, without discomfort.
  2. We are more rational about the human languages than about the programming languages.

Andy Hunt and Dave Thomas of Pragmatic Programmers have suggested a practical solution to this over-attachment: Learn a new programming language every month. My solution is an easier variation of that.

Toolbox Programming


In my method, there is no need to pick up a new language every month, year, or even every decade.
But when you pick up new ones, don't limit yourself to one, pick up as many new ones as you can.

When do you need to pick up new languages?
  1. When your guy/gal is giving you trouble, when you start to feel pain. Example: the language is beautiful, the framework is beautiful, but you cannot deploy your app using them. Don't suffer the pain, switch.
  2. When you decide that your guy/gal is perfect and that you cannot imagine your life without him/her. No one is perfect and s/he will forsake you someday and thus you'd better start finding some substitutes now.

What new languages do you pick up?
Since you are head over heel in love with this particular language, find as many languages similar to that one as you can.

You love it because it is a Functional language? Find 3 or 4 other Functional languages.

You love it because it enables you to write very small, compact programs. Find 3 or 4 other languages with such concise syntax and conventions.

You love it because it runs on JVM? Fine 3 or 4 other languages that can run on JVM as well.

Conclusion


Just after a few days each with Scala and Clojure, our Ruby Tutor has cured himself of his severe case of Ruby infatuation. Of course, he won't start using Scala or Clojure next week or month. He may not even use them at all. But he now knows he can hope for something Ruby-like if Ruby and its frameworks really prove nasty and stubborn. He won't grovel, kowtow, degrade himself, but switch, or wait, or both, or something else.

Fall in love, it's a fine noble sentiment. But not madly, not with the wrong types.

Once you find you are thinking it will be the end of the world to lose, or break up with that language,

Well, go ahead and pick up at least 3 new tools!

January 14, 2013

Lean Integration | an Appetizer Book Review

I am referring to Lean Integration.

It looks like a nice book. Maybe I need to read it again for more insights and knowledge. Here I share with you what I found to be most delicious morsels in it.

Techniques for Enabling Constant Change

  1. Break up dependencies.

  2. Make decision reversible.

  3. Maintain "live" documentation.

Break up dependencies


Recall what Joel Spolsky admired about Microsoft Excel Team's guts? They hated dependencies so much that they even did their own compiler.

If you have time and resource, you should consider doing your own O/S, database, etc.

Make decision reversible.


Is this another XP(Extreme Programming) mantra?
Decisions small enough, separate/isolated/orthogonal enough, to reverse at small costs?

Maintain "live" documentation.


Is this what Bruce Tate in Better Faster Lighter Java calls "Transparency"?
A sort of self-documenting, self-explanatory, self-descriptive code?


Startup's Point of View


With minimal resources, software startups have little choice but to reuse third-party codes. Almost all of startup projects are exercises in integration.

Thus integration skills are strategic for startups.

January 11, 2013

Programming - The Answer , My Friend, is ...

on August 11, 2011

My Own Failures


1. I wrote an application in a Ruby web framework 1.x.x. Everything worked, fine. Then the 2.x.x. version came along and I upgraded and my app broke in many places. I fixed them somehow.

Then the 3.x.x. version came along and I upgraded and my app broke in many places again.

That is not the framework's problem. The real problem is I had used too many plugins.

2. I wrote an application in a PHP web framework 1.6.x. Everything worked, fine. Then the 1.7.x. version came along and I upgraded and my app broke in many places.

This is not the framework's problem. The real problem is I had used too many PEAR's.

3. Before all this, I worked for a project that migrated from EJB1 to 2 to 3 etc.
And for another project, where the project managers took years to move from Struts 1 to 2.

Other Failures


1. Netscape's spaghetti code that cost the company its battle against Microsoft.
2. Friendster's social graph that slowed the site to a crawl.
3. MySpace's "technical debt" that ruined the site.
4. Ruby on Rails framework --- Its lack of hosting (Maybe their developers don't care or it is their blind spot). And its handling of data management; if they don't know this part well enough, they should leave it alone e.g. PHP has no ORM and has been doing just fine.
5. Microsoft alone has a dozen or more data access technologies from 1998 to 2008, according to a book by Joel Spolsky. I don't have time or experience to know such stuffs.

My Own Successes


1. Porting RedBlack tree, AA tree etc from Java to Ruby is easy.
2. Porting shortest path, quick sort etc from Ruby to Python is easy.
3. Porting Regular expressions, string/text handling etc from PHP and Perl to Ruby is easy.
4. Porting higher order functions amongst Perl, Python, Ruby is easy.
5. Porting any plain domain or business object from any language to any other language is very very easy. From Java to Pascal, from Perl to PHP, from Ruby to C++, whatever.

6. Modeling or making mental representations or doing objects, classes and APIs for Law domains, Insurance domains, Education domains are very very very easy and fast.

7. Dreaming, going into trance, seeing in my mind eyes some apps end to end from the users' point of views is extremely easy and fast. "Easy" doesn't mean I do these activities well because, often, I even forgot to do them at all!

8. Writing domain objects is very easy.

9. Combining those objects is very easy.

Other Successes


1. Real programmers code everything by themselves. A paradox because they do and they don't.
2. Real programmers don't trust others' codes. A paradox because they do and they don't.

3. Excel team wrote even their own compiler, and killed off all dependencies.
4. Algorithms are very infrequent. They come once or twice in a decade.

5. Facebook is using many programming languages. So is Twitter.
6. Google founders didn't learn HTML etc on purpose.


The answer


If you can't find Apple-style appealing packaging, sell your stuffs, your wares raw, plain, uncovered!

Have fun!

January 9, 2013

From "Code and Pray" to "Crash and Fix"

August 30, 2010

Software industry is changing every hour and making progress every day. Software development methods and techniques are also getting better every year. Thus, we old folks have to stay updated and one way to achieve this is to learn from the apprentices and the interns.

This week I like to discuss different intern techniques: those in our days and those nowadays. In our intern days, one common technique was "code and pray." We would code something and run it by the complier and pray that it worked. Some practictioners also call this coincidental style of coding.

Recently I noticed that the interns at our company have developed this to a higher level. I noticed that they are now using "crash and fix" technique. It's a beauty in this particular case.

Crash and Fix GQL


As we all know, we need to create and provide an index.yaml file to Google App Engine so that it can search its data store properly.

Locally (that is on our development server), if we make a new kind of query, the Engine updates the index file. When we repeat that query some time later, the Engine is ready to do the search.

However, on the real server, if we make a new kind of query, the Engine just scans the index file. If the index necessary for the query is not there, the Engine throws exceptions.

There is no quota/limit on the local engine, but the real engine places some limit on the number of indexes.

Thus, the right thing to do, at least for us old folks, is:

  1. Find typical usage patterns.

  2. Make queries for just these patterns.

  3. The resultant index.yaml file, developed on the local server, will be relatively small.

  4. Use this small index.yaml file on the real server,


This would take time and insight. Modern interns have better ideas:

  1. Deploy the app on the real server with an empty index.yaml file.

  2. When a user makes a new type of query, the Engine throws excpetions. Log those exceptions somewhere.

  3. Check those logs. The log tells us the specifications of the index it needs for a particular query..

  4. Copy such specifications into a new index.yaml file.

  5. Deploy the app on the real server again, and repeat.
This also took time but not insight. Do the modern interns have an edge over us old folks? Pay special notice to the comment "# goodpt3," "# goodpt2" etc. Those below 1 were added before those below 2 and so on. The interns will check only the topmost section, which is added the most recently. Time-saving and brain-saving, cool?

indexes:

# AUTOGENERATED

# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run. If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED"). If you want to manage some indexes
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.

...

- kind: Inventory
properties:
- name: expenses
- name: locations
- name: distance
- name: skus
- name: unit_space
direction: desc
- name: transports
...

- kind: Inventory
properties:
- name: gender
- name: name
- name: distance
- name: skus
- name: unit_space
direction: desc
- name: transports

# goodpt3
...
- kind: Inventory
properties:
- name: unit_space
- name: gender
- name: locations
- name: phone
- name: skus
- name: transports
...


# goodpt2

...

- kind: Inventory
properties:
- name: unit_space
- name: expenses
- name: gender
- name: locations
- name: phone
- name: distance
- name: skus
- name: transports

# goodpt1 - kind: Inventory
properties:
- name: education
- name: transports
- name: gender
- name: locations
- name: skus
- name: unit_space
direction: desc
...

- kind: Inventory
properties:
- name: transports
- name: gender
- name: locations
- name: distance
- name: skus
- name: unit_space
direction: desc
...

The file has almost 1200 lines and uses up 92% of the index quota. If we add new models/entities, we must make new queries and will need new indexes. Will the remaining 8% cover most of our future needs, or am I over-planning?

An Online Programming Community and their Members


Here is a verbatim invitation to the  Singapore Python User Group on Facebook.


I admire the source code of Django, Tornado, NLTK, boto, etc. As many of the world greatest coders in history advise, their logic/code is "dumb" and therefore good.

When I coded shopping carts, CRUD websites etc, my logic/code stayed "dumb" and good. But when I moved even a bit farther, e.g. FSM with Pygame, an expert system etc, my logic/code became "clever," complex and unstable.

I've thought about and studied this for years.

Our team is training to be a world leader in software -- both biz-wise and techno-wise. Anyone interested in becoming better programmers are welcome.

We are humble to learn and generous to share.

Love and Thanks to all.


I'll let you see and hear how they respond in a few days' time.

January 1, 2013

Revolutions in Artificial Intelligence and Me ;-)

I am not interested in "hard" things. I don't want other people to do "hard" things either. So, when I read about previous AI revolutions and learned about their "hard" problems, I was not inspired, to say the least.
  1. Tough to do even a tic tac toe problem.
  2. Tough to overcome "knowledge bottleneck".
  3. Tough to encode "commonsense", as demonstrated by CYC project.
  4. Machines will learn on their own, but you have to train them first, and it's Tough to train them.

But when Google fans claimed what it is doing is a sort of AI, I became inspired a bit because this brand of AI seems accessible and easier to do. Even then, I took no action actually.

Now, AI seems to have entered a new epoch. Some AI researchers claimed in a New Scientist article that they've done "with relatively little programming" what it had been tough for older kinds of AI. They labelled their kind of AI as "morphological computing." Here I explain one of their most illuminating examples.

1 snail is boring.
10 snails are still boring.

1 octopus is interesting.
10 octopuses are still more interesting.

Rich behaviours, idiosyncratic behaviours, responsive actions.

Are octopuses more "intelligent" than snails?
Are octopus brains more "intelligent" than those of snails?

The researcher knew that this can't be because he also knew that both of them are "mollusc." According to him, the octopus also has no brain.
Another useful link.

What excites me most is that an entity can be made to look, sound, seem, feel intelligent while in reality it has no real intelligence.(An aside: this is far better than Searle's Chinese Room interpreter.)

What inspires me most is that I don't need to design/code/program/write a brain to make my software behave intelligent.



That's, for a programmer, a bit too much even to dream about and wish for!

I share. Please use it for your benefits.

on January 27, 2012

December 23, 2012

PayPal IPN, Google App Engine, Python Integration, an Example

References


1. PayPal IPN Python Code from Django Aware blog.
2. A Google Group discussion

3. Pro PayPal E-Commerce. I recommend this book.
4. Professional Web APIs with PHP. This book is good for non-PayPal stuffs too. A good book.

My Tutorial


(1) Make a sandbox/testing account with PayPal.
(2) Log in to that account. Get to IPN Simulator at the bottom of the page.
(3) In the form that shows up, make sure
1. notify url = http://your_app.appspot.com/path-to-your_ipn_handler
2. "receiver_email" = "ACCOUNT_EMAIL" that you use in your IPN handler code.
3. You should vary "payment_status" from "Completed" to "Failed" to "Pending" etc.
Send the form a few times.
(4) Paypal says it can't send.
Change " notify url" to www.google.com etc. You get the same failure result.
It is as expected: you haven't written the code to handle this POST request.

The code you need to write is:


import os import sys import logging import wsgiref.handlers from google.appengine.ext import webapp from google.appengine.ext.webapp import template from util.sessions import Session from google.appengine.ext import db sys.path.append(os.path.join(os.pardir,os.getcwd())) from helper_rendering import doRender import urllib, urllib2 from google.appengine.api import mail from google.appengine.api import urlfetch from google.appengine.ext.webapp.util import run_wsgi_app PP_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr" ACCOUNT_EMAIL= "my_email_for_sandbox@nice.com" #PP_URL = "https://www.paypal.com/cgi-bin/webscr" #ACCOUNT_EMAIL = "my_email_for_real-paypal_acct@good.com" class IPNHandler1(webapp.RequestHandler): def post(self): parameters = None # Check payment is completed, not Pending or Failed. if self.request.get('payment_status') == 'Completed': if self.request.POST: parameters = self.request.POST.copy() if self.request.GET: parameters = self.request.GET.copy() logging.debug('IPN 1, or your own message for yourself to read in log.') else: self.response.out.write('Error, sorry. The parameter payment_status was not Completed.') # Check the IPN POST request came from real PayPal, # not from a fraudster. if parameters: parameters['cmd']='_notify-validate' params = urllib.urlencode(parameters) status = urlfetch.fetch( url = PP_URL, method = urlfetch.POST, payload = params, ).content if not status == "VERIFIED": self.response.out.write('Error. The request could not be verified, check for fraud.') parameters['homemadeParameterValidity']=False # parameters = None # You may log this data in your database, # for later investigation. # Check the money is really to go to your account, # not to a fraudster's account. if parameters['receiver_email'] == ACCOUNT_EMAIL: transaction_id = parameters['txn_id'] # Check if this is a new, unique txn, # not a fraudster re-using an old, verified txn. invoice_id = parameters['invoice'] currency = parameters['mc_currency'] amount = parameters['mc_gross'] fee = parameters['mc_fee'] # Check if they are the right product/item, right price, # right currency, right amount, etc. email = parameters['payer_email'] identifier = parameters['payer_id'] # Email/notify/inform the user for whatever reason. parameters['your_parm']= "It is ok on 19 September, 2010." logging.debug('IPN 100. All OK.') logging.debug(parameters['txn_id']) logging.debug(parameters['invoice']) logging.debug(parameters['payer_email']) # With this IPN testing, you can't see results on the browser. # See results on the log file maintained by Google AppEngine. #template_values = { 'params': parameters, } #doRender(self,'ipn_ok.htm', template_values ) if __name__ == '__main__': print "IPNHandler1 is being run as main()."

(1) Now send the IPN Simulator form again as explained in the beginning of this post.
Now Paypal says it has sent it successfully.
(2) Log in to your Google AppEngine developer account.
Get to the log. Set logging level to "debug"
Check the log entries. you will see something like:


1. 09-29 01:54AM 28.745 /ipn_handle1 200 2141ms 408cpu_ms 4kb gzip(gfe) See details 216.113.191.33 - - [29/Sep/2010:01:54:30 -0700] "POST /ipn_handle1 HTTP/1.1" 200 4842 - "gzip(gfe)" "yourstruly.appspot.com" ms=1234 cpu_ms=123 api_cpu_ms=0 cpm_usd=0.123456 loading_request=1 2. D 09-29 01:54AM 29.395 IPN 1 3. D 09-29 01:54AM 30.682 IPN 100. All OK. 4. D 09-29 01:54AM 30.682 31929814 5. D 09-29 01:54AM 30.682 abc1234 6. D 09-29 01:54AM 30.682 buyer@alan.htet.com 7. I 09-29 01:54AM 30.705 Creating session session-575150356408086346 8. I 09-29 01:54AM 30.884 This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request ...

If you have any question, just ask by way of commenting below.

December 22, 2012

Google Query Language, General Patton and an Apprentice

on August 21, 2010

Topics: Google Query Language, General Patton and an apprentice at our software startup.

Google as you know allows its employees to take 20% of their time to do their own pet projects: 80% for the company and 20% for their own.

Our company? Yes, our ratio is almost the reverse, especially for the interns and the apprentices. They take 80% of their time to do their own pet projects and their own learning: 20% for the company and 80% for their own. So, they put in about 2 hours per day, out of 10 hours working time (10 hours include 2 hour lunch time and other breaks!)

Even then, I wonder, "Is this apprentice wasting our time?"

And General Patton? The famous American WW II tank commander? What does he have to do with software?

The answer: a lot.

If impatient, just go leap to the conclusion.

Example 1


from google.appengine.ext import db
from models.tutor import Residence
import inspect

query = db.GqlQuery("SELECT * FROM Residence WHERE "
"dimensions IN :mydimensions "
"AND locations IN :mylocs "
"AND rent < :myrent "
"AND gender = :mygender"
,
mydimensions =["30x60", ],
mylocs = ['W', "W"],
myrent = 41,
mygender= "M"
)
print inspect.getmembers(query)

which gives the following result:

[('__class__', ),
('__delattr__', ),
('__dict__', {'_namespace': None, '_proto_query': , '_kwds': {'mylocs': ['W', 'W'], 'mydimensions': ['30x60'],
'myrent': 41, 'mygender': 'M'}, '_args': [], '_model_class': ,
'_keys_only': False, '_cursor': None, '_end_cursor': None, '_compile': True}),
('__doc__', 'A Query class that uses GQL query syntax instead of .filter() etc.'),
('__getattribute__', ),
('__getitem__', bound method GqlQuery.__getitem__ of >), ('__hash__', ),
('__init__', bound method GqlQuery.__init__ of ('__iter__', bound method GqlQuery.__iter__ of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c>>), ('__module__', 'google.appengine.ext.db'),
('__new__', built-in method __new__ of type object at 0x8146a20),
('__reduce__', built-in method __reduce__ of GqlQuery object at 0xaa7dd6c),
('__reduce_ex__', built-in method __reduce_ex__ of GqlQuery object at 0xaa7dd6c),
('__repr__', method-wrapper '__repr__' of GqlQuery object at 0xaa7dd6c),
('__setattr__', method-wrapper '__setattr__' of GqlQuery object at 0xaa7dd6c),
('__str__', method-wrapper '__str__' of GqlQuery object at 0xaa7dd6c), ('__weakref__', None), ('_args', []), ('_compile', True), ('_cursor', None), ('_end_cursor', None),
('_get_query', bound method GqlQuery._get_query of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c),
('_keys_only', False), ('_kwds', {'mylocs': ['W', 'W'],
'mydimensions': ['30x60'], 'myrent': 41, 'mygender': 'M'}), ('_model_class', class 'models.tutor.Residence'),
('_namespace', None),
('_proto_query', google.appengine.ext.gql.GQL object at 0xaa7d6ac),
('bind', bound method GqlQuery.bind of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c),
('count', bound method GqlQuery.count of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c),
('cursor', bound method GqlQuery.cursor of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c),
('fetch', bound method GqlQuery.fetch of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c),
('get', bound method GqlQuery.get of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c),


('is_keys_only', bound method GqlQuery.is_keys_only of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c), ('run', bound method GqlQuery.run of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c), ('with_cursor', bound method GqlQuery.with_cursor of google.appengine.ext.db.GqlQuery object at 0xaa7dd6c)]

Example 2


from google.appengine.ext import db
from models.tutor import Residence
import inspect

query = db.GqlQuery("SELECT * FROM Residence WHERE "
"dimensions IN :mydimensions "
"AND locations IN :mylocs "
"AND rent < :myrent "
"AND gender = :mygender"
,
mydimensions =["30x60", ],
mylocs = ['W', "W"],
myrent = 41,
mygender= "M"
)

print query.__dict__


which gives the following result:

{'_namespace': None, '_proto_query': , '_kwds':
{'mylocs': ['W', 'W'], 'mydimensions': ['30x60'], 'myrent': 41, 'mygender': 'M'}, '_args': [],
'_model_class': class 'models.tutor.Residence', '_keys_only': False, '_cursor': None,
'_end_cursor': None, '_compile': True}

Example 3


from google.appengine.ext import db
from models.tutor import Residence
import inspect

query = db.GqlQuery("SELECT * FROM Residence WHERE "
"dimensions IN :mydimensions "
"AND locations IN :mylocs "
"AND rent < :myrent "
"AND gender = :mygender"
,
mydimensions =["30x60", ],
mylocs = ['W', "W"],
myrent = 41,
mygender= "M"
)

obj1= query.__dict__['_proto_query']
print obj1


which gives the following result:

google.appengine.ext.gql.GQL object at 0xaa7814c

Example 4


from google.appengine.ext import db
from models.tutor import Residence
import inspect


query = db.GqlQuery("SELECT * FROM Residence WHERE "
"dimensions IN :mydimensions "
"AND locations IN :mylocs "
"AND rent < :myrent "
"AND gender = :mygender"
,
mydimensions =["30x60", ],
mylocs = ['W', "W"],
myrent = 41,
mygender= "M"
)

obj1= query.__dict__['_proto_query']
print obj1.__dict__['_GQL__symbols']


which gives the following result:

['SELECT', '*', 'FROM', 'Residence',
'WHERE', 'dimensions', 'IN', ':mydimensions',
'AND', 'locations', 'IN', ':mylocs',
'AND', 'rent', '<', ':myrent',
'AND', 'gender', '=', ':mygender']


which gradually leads to

['SELECT', '*', 'FROM', 'Residence',
'WHERE', 'name', '=', ':myname',
'AND', 'email', '=', ':myemail',
'AND', 'phone', '=', ':myphone',
'AND', 'rent', '=', ':myrent',
'AND', 'neighborhood', '=', ':myneighborhood',
'AND', 'transportation', '=', ':mytransportation',
'AND', 'gender', '=', ':mygender',
'AND', 'race', '=', ':myrace',
'AND', 'dimensions', 'IN', ':mydimensions',
'AND', 'locations', 'IN', ':mylocations',
'ORDER', 'BY', 'email', 'DESC', ',', 'rent', 'ASC']

Conclusion


At one time in WW II, the rumors (which was actually true) went that the German tanks were far stronger than the American tanks. General Patton must have known this before anyone else.

Did he admit the truth? No.

Did he lie and refute the truth? No.

What did he say? He said in effect that he never intended to use his tanks that way. His tanks were to do something else, not ram/bump/jar into or out-punch/out-slug/out-shoot the opponent tanks. Read more about that here.

The younger software developer wants to look into the nuts and bolts of a tank (a deliberately mixed metaphor).
The older General just wants him to look into the logic of his query in pseudo-code or read the doc for GQL(mixed again).

Engineers and soldiers
programmers and suits
engineers and marketers

Anyway, we gave the apprentice a pat on the back. Whether he works his 2 hours as Engineer or Soldier, is fine with us.

December 21, 2012

Meta-programming in Ruby : Part 16 : instance_eval

There are altogether 16 posts on this topic of meta-programming in Ruby language. Our approach encourages hands-on experimentation. The exercises here are specially designed to facilitate self-study and other independent modes of learning.

Provided we can find time amidst our busy schedule(well, who is not busy these days?), we will also discuss meta-programming in other languages such as Lisp and python.

Code 1

instance_eval and self.


# instance_eval evaluates a block of code
# with self set to the object that's receiving the instance_eval call.

add_two = Proc.new { self + 2 }

puts 1.instance_eval(&add_two) #=> 3
puts 2.instance_eval(&add_two) #=> 4

"stellar coding staff".instance_eval{p self}
[1970, 1980, 1990, 2000].instance_eval{p self}
{'a'=> 1970, 'water'=> 1990, 'gold'=>2010}.instance_eval{p self}
puts "\n\n"


# Here's a second example using an implicit method receiver.
call_reverse = Proc.new { self.reverse }

p "alan akhoe kid".instance_eval(&call_reverse)
p ["x", "y", "z"].instance_eval(&call_reverse) #=> ["z", "y", "x"]
p [1, 34, 117, 997].instance_eval(&call_reverse)
# OK BAD # p 12121.instance_eval(&call_reverse)


Result 1



[root@localhost ruby_tutor]# ruby ie_self.rb
3
4
"stellar coding staff"
[1970, 1980, 1990, 2000]
{"a"=>1970, "water"=>1990, "gold"=>2010}


"dik eohka nala"
["z", "y", "x"]
[997, 117, 34, 1]


Code 2

We can display private attributes using instance_eval.


class C
def initialize
@a = 0
end
def next
@a += 1
end
end
w = C.new
p w.next
p w.next; puts;puts

p w.instance_eval "@a" #=> 2
p w.instance_eval { @a } #=> 2
p C.new.instance_eval { @a } #=> 0


Result 2



[root@localhost ruby_tutor]# ruby ie_and_private_vars.rb
1
2


2
2
0


December 18, 2012

Meta-programming in Ruby : Part 2 : Method Missing


Code 1


class Hash
def method_missing(m,*a)
if m.to_s =~ /=$/
self[$`] = a[0]
elsif a.empty?
self[m]
else
raise NoMethodError, "#{m}"
end
end
end

x = {'abc' => 123}
p x['abc']
print "\n"

x.foo = :baz
x.goo = "duh"
x.x&z = 10
p x


Result 1


[root@localhost ruby_tutor]# ruby method_missing1.rb
123

{"abc"=>123, "foo"=>:baz, "goo"=>"duh"}


Code 2


class Hunt
@@arr = []
def show;
@@arr;
end

def method_missing(name, *args)
begin
@@arr.send(name, *args)
rescue
"'#{name}' method has NOT been implemented YET"
end
end
end

he = Hunt.new

he << 123
he << "Be happy, Son!"
p he.show
puts he.flyswim


Result 2


[123, "Be happy, Son!"]
'flyswim' method has NOT been implemented YET



Code 3
Remember that method_missing will provide a block if one is given


def method_missing(name, *args, &block)
block.call(*args) if block_given?
end

no_method("Ask","why", "study this !"){ |*args| p args }



Result 3


[root@localhost ruby_tutor]# ruby method_missing_bloc_CROSS-REF.rb
["Always demand", "why you have to", "study this or that or whatever!"]


Meta-programming in Ruby : Part 1 : Alias


There are altogether 16 posts on this topic of meta-programming in Ruby language. Our approach encourages hands-on experimentation. The exercises here are specially designed to facilitate self-study and other independent modes of learning.

Provided we can find time amidst our busy schedule(well, who is not busy these days?), we will also discuss meta-programming in other languages such as Lisp and python.

Code 1
class A
 def fan;
     puts "fan";
 end
def feast;
     puts "feast";
 end
end
a= A.new
a.fan
a.feast
class A
 alias :flank :fan
 alias :foo :feast
 private :fan
end
begin
a.fan
rescue
 puts ">>>>>>   private method `fan' called for #",
      ">>>>>>   Accessible unless declared \"private\" as above"
end
a.feast
a.foo
a.flank
puts "\n\n"

class Z
 attr_accessor :gear
 alias :get_gear    :gear
 alias :set_gear    :gear=
end

zoe =Z.new
zoe.gear = 34
puts zoe.gear

zoe.set_gear(34)
puts zoe.get_gear 


Results 1
[root@localhost ruby_tutor]# ruby 1alias.rb
fan
feast
>>>>>>   private method `fan' called for #
>>>>>>   Accessible unless declared "private" as above
feast
feast
fan


34
34


Code 2
class String
   alias_method :original_reverse, :reverse

   def reverse
     "Trying to reverse, please wait ... "+ original_reverse
   end
end
p "happy holy days".original_reverse

p "happy holy days".reverse


Results 2
[root@localhost ruby_tutor]# ruby   2alias_method1.rb
"syad yloh yppah"
"Trying to reverse, please wait ... syad yloh yppah"


A Tip for Job Search: Gold Rush Skills

  If you need to make some money very quickly, what would you do? Your answer points to the kind of problems you can solve. They give you so...