Thursday, November 1, 2012

How to join strings, format strings... add variables to strings... printf?


Javascript dynamic string manipulation.

How to join strings, format strings... add variables to strings... printf?


It has nothing as neat as Python's... but you'll find a way when you need to.



How to change color/style of element in Jquery/Javascript


jQuery Learnings.

any styling you can do with CSS you can handle with jQuery.



Place the following in your jQuery mouseover event handler:
$(this).css('color', 'red');To set both color and size at the same time:
$(this).css({ 'color': 'red', 'font-size': '150%' });You can set any CSS attribute using the .css() jQuery function.



Whatever DOM element you turn into a jQuery object using this constructor $(),
you can use the .css() method on it.



2

$(document).ready(function() {
    var button = $('#specific_consultant_no');
    button.click(function() {
        button.css('color', 'FOO');
    });
});

THIS
$(this) = in the function object, 'this' refers to: the DOM element, which you need to turn into a jquery object by using $() on this.
[then only you get all the jquery object goodness as .methods on that element.]


if you want to reuse the method, you can do eg.
var colorMethod = function() {
    $(this).css('color', 'FOO');
};
$(document).ready(function() {
    var button = $('#specific_consultant_no');
    button.click(colorMethod);
});
you can also use the addClass and removeClass methods, to be more flexible!



This lets you name your functions to be reused elsewhere.

var colorMethod = function() {

Define using var and give it a name, actually is closest to Python
function_obj = lambda a, b : a+b

or def asdf(a, b): return a+b
function_obj = asdf
[In this second case, you could just pass asdf as arg to a function.]



Entire Jquery object?

You could use the css function:
$(function() {
    $('#specific_consultant_no').click(function() {
        $(this).css('color', 'red');
    });
);




How to change color/style of element in Jquery/Javascript
(just, glorified console-text formatting again.)

changing text color with jquery - Google Search


If no jquery:
http://www.ehow.com/how_5220878_change-text-color-javascript.html
document.getElementById('mySpan').style.color = '#FFFFFF';

function spanColorChange(){document.getElementById('mySpan').style.color = '#FFFFFF';}





Python functions modify whatever arguments you pass in.


Python functions modify whatever arguments you pass in,
and the changes are seen outside the function.

Example code:


dct = dict(a='a', b='b')

def modify_dct(thedct):
    thedct['a'] = 1
    thedct['b'] = 2

if __name__ == '__main__':
    print dct
    modify_dct(dct)
    print dct
    print "OK"



Output:


[Dbg]>>> 
{'a': 'a', 'b': 'b'}
{'a': 1, 'b': 2}
OK



What just happened?
Unlike C, which has pass by value, whatever you pass to a function other than primitives (int, e.g. 1, 110,)
is actually a reference to that object itself, and not a copy.

So, modifying it inside the function will see it changed outside the function, after the func has run (processed it) on it.





linux shell script how to check environment variable exist (and define a default if it doesn't)


linux shell script how to check environment variable exist - Google Search

Scenario: You have a long command line e.g.
python sth.py --asdf $asdf --qwer $qwer --zxcv $zxcv

and you're getting the $xxxx from somewhere else.. but it could be empty..
in which case all your --xxxx get --yyyy as their arguments... not good...



linux shell script how to check environment variable exist (and define a default if it doesn't)
- 2 syntax (OLD, NEW)



http://www.cyberciti.biz/faq/bash-ksh-if-variable-is-not-defined-set-default-variable/

output=${1-text}
echo $output

Common mistake:
$output=${1-text}
If you do this, errmsg is := not defined not found etc. (becoz left hand side is blank)
Correct is this
output=${asdf-text}
or
output=${output-text}
Note, without the $ sign in front


Or, Syntax 2:

OR (see my comment below):
output=${1:-text}
echo $output




Vivek Gite July 27, 2009 at 7:59 am
@Ronald,
The original Bourne shell only supported above syntax and it works with all shells to keep portability. POSIX shells (KSH and BASH) offer a slight variant (as mentioned in bash man page):
${1:-text}
I should have mentioned both syntax..
HTH




Ronald Fischer July 27, 2009 at 7:11 am
It surprises me that this works (and, at least, on bash 3, it does). I have always used the form
${VAR:-VALUE} in such a case, not ${VAR-VALUE}, but it seems that both work. The man pages of bash describe, however, ONLY the variant with a colon, so I wonder whether omitting the colon just exploits an undocumented feature, which might be gone with the next version of bash. Or did I miss here something in ‘man bash’?




Shell Scripting: If Variable Is Not Defined, Set Default Variable



If var is defined AND NOT EMPTY, use var, otherwise set a default variable under Bash. For e.g. my script needs a parameter for output variable. It can be text or html. I set it as follows in my script







2
http://stackoverflow.com/questions/2981878/bash-checking-for-environment-variables

In Bash (and ksh and zsh), if you use double square brackets you don't need to quote variables to protect against them being null or unset.
$ if [ $xyzzy == "x" ]; then echo "True"; else echo "False"; fi
-bash: [: ==: unary operator expected
False
$ if [[ $xyzzy == "x" ]]; then echo "True"; else echo "False"; fi
False
There are other advantages.






3: useful
What's the best way to check that environment variables are set in Unix shellscript
http://stackoverflow.com/questions/307503/whats-the-best-way-to-check-that-environment-variables-are-set-in-unix-shellscr

Your question is dependent on the shell that you are using.
Bourne shell leaves very little in the way of what you're after.
BUT...
It does work, just about everywhere.
Just try and stay away from csh. It was good for the bells and whistles it added, compared the Bourne shell, but it is really creaking now. If you don't believe me, just try and separate out STDERR in csh! (-:
There are two possibilities here. The example above, namely using:
${MyVariable:=SomeDefault}
for the first time you need to refer to $MyVariable. This takes the env. var MyVariable and, if it is currently not set, assigns the value of SomeDefault to the variable for later use.
You also have the possibility of:
${MyVariable:-SomeDefault}
which just substitutes SomeDefault for the variable where you are using this construct. It doesn't assign the value SomeDefault to the variable, and the value of MyVariable will still be null after this statement is encountered.
HTH.
cheers,
Rob








how do you reset CSS localized in your small Universe of your ?

Scenario:

1
What to do if you're using some CSS layout by somebody else,
and you want to style differently at certain parts, BUT...

the original CSS already styled stuff like <p> and <div> generally...

how do you reset CSS localized in your small Universe of your <block> ?


2 what is the CSS syntax actually?

Why does this work
p.opening-intro
and this doesn't
.opening-intro p


And why does this work
#opening-jqm-homeheader p
and this doesn't?
p #opening-jqm-homeheader
p#opening-jqm-homeheader

What is the space combinator, and the , (comma) combinator?





http://stackoverflow.com/questions/9515534/disinherit-reset-the-css-style-of-a-specific-element

This is a problem that is solved best by avoiding it from the beginning. I try to keep contextual (or decendeant) selectors to a minimum and I avoid using tag names as selectors. Instead I make use of classes so that my html elements (<a>, <p>, <ul>, <span>, etc) will always look like they've not been styled no matter what the context/its parent element is.
In your case, I think you can only overwrite the inherited styles as you have mentioned with the inline-style attribute or with !important or even better, create a .reset class.
.reset { with: auto; height: auto; padding: 0; /* etc */ }
All solutions mentioned above have drawbacks, so you'll need to chose your battle.


Contextual/descendent selectors =
http://reference.sitepoint.com/css/descendantselector



Q. Ok, I have a beautiful web, with its styles within a CSS and everything
But now I've found a problem, I want ONE list to be virgin, without any style inherited.
I know I can do it just giving it a style="(...)" so it overwrites the inherited style, but, is there any instruction / trick / something to do it without doing this?

Solution to #1, just make a localized css style block next to the new html block you put in, for ease of testing:

    <style type="text/css">
p.opening-intro {
    border-top: 1px solid #75AE18;
    font-size: 0.96em;
    line-height: 1.3;
    padding: 0.5em 1em 0;
color: #558E08;
}
#opening-jqm-homeheader p {
    color: #666666;
    font-size: 0.9em;
    font-weight: bold;
    line-height: 1.3;
    margin: 0.3em 1em 0;
}
#opening-jqm-homeheader {
    margin-bottom: 1em;
}
    </style>

http://stackoverflow.com/questions/8228980/reset-css-display-property-to-default-value



2
http://snipplr.com/view/7438/element-specific-css-reset/
http://perishablepress.com/a-killer-collection-of-global-css-reset-styles/
css specific reset
http://weblogs.asp.net/bleroy/archive/2009/04/14/css-isolation-there-has-got-to-be-a-better-way.aspx
how to locally reset css






Wednesday, October 31, 2012

git how to get all branches into existing local repo?

git how to get all branches into existing local repo?

Git: How to get all branches of remote repo into existing local repo, WITHOUT using git clone into a new directory? (Because you just want to get the diff, and not have to re-download every single thing -__-")


http://www.kernel.org/pub/software/scm/git/docs/git-clone.html

GIT URLS

In general, URLs contain information about the transport protocol, the address of the remote server, and the path to the repository. Depending on the transport protocol, some of this information may be absent.
Git natively supports ssh, git, http, https, ftp, ftps, and rsync protocols. The following syntaxes may be used with them:
  • ssh://[user@]host.xz[:port]/path/to/repo.git/
  • git://host.xz[:port]/path/to/repo.git/
  • http[s]://host.xz[:port]/path/to/repo.git/
  • ftp[s]://host.xz[:port]/path/to/repo.git/
  • rsync://host.xz/path/to/repo.git/
An alternative scp-like syntax may also be used with the ssh protocol:
  • [user@]host.xz:path/to/repo.git/
The ssh and git protocols additionally support ~username expansion:
  • ssh://[user@]host.xz[:port]/~[user]/path/to/repo.git/
  • git://host.xz[:port]/~[user]/path/to/repo.git/
  • [user@]host.xz:/~[user]/path/to/repo.git/
For local repositories, also supported by git natively, the following syntaxes may be used:
These two syntaxes are mostly equivalent, except the former implies --local option.
When git doesn’t know how to handle a certain transport protocol, it attempts to use the remote-<transport> remote helper, if one exists. To explicitly request a remote helper, the following syntax may be used:
  • <transport>::<address>
where <address> may be a path, a server and path, or an arbitrary URL-like string recognized by the specific remote helper being invoked. See git-remote-helpers(1) for details.
If there are a large number of similarly-named remote repositories and you want to use a different format for them (such that the URLs you use will be rewritten into URLs that work), you can create a configuration section of the form:


2
Git for human beings


3
http://stackoverflow.com/questions/67699/how-do-i-clone-all-remote-branches-with-git

If you have many remote branches that you want to fetch at once:
$ git remote update
$ git pull --all
Now you can checkout any branch as you need to, without hitting the remote repo.


But it doesn't seem to do what was asked... If you run those commands and then run "git branch" you will still only see the branches that you had to begin with. – infosec812 Oct 27 '11 at 17:13


4
http://stackoverflow.com/questions/6373277/git-sync-local-repo-with-remote-one

I'm a git noob so this might look like a silly question.
I have local & remote git repositories. I want to sync my local repository with remote repository so that my local repository becomes 100% copy of remote repository. Meaning that if certain files differ in these repos we override local ones with remote ones. And if there are files in local repos that do not exist in remote repo, local files get removed.
Is there a way to achieve that other than doing a fresh clone of remote repo?





7
what is git fetch
http://stackoverflow.com/questions/292357/whats-the-difference-between-git-pull-and-git-fetch

In the simplest terms, git pull does a git fetch followed by a git merge.
You can do a git fetch at any time to update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy. I have even heard of people running git fetch periodically in a cron job in the background (although I wouldn't recommend doing this).
A git pull is what you would do to bring your repository up to date with a remote repository.


5
http://blog.mikepearce.net/2010/05/18/the-difference-between-git-pull-git-fetch-and-git-clone-and-git-rebase/

Git rebase

Finally, git rebase is pretty cool. Anything you’ve changed by committing to your current branch but are no in the upstream are saved to a temporary area, so your branch is the same as it was before you started your changes, IE, clean. It then grabs the latest version of the branch from the remote If you do ‘git pull –rebase’, git will pull down the remote changes, rewind your local branch, then replays all your changes over the top of your current branch one by one, until you’re all up to date. Awesome huh?

Finally..

If you get stuck, run ‘git branch -a’ and it will show you exactly what’s going on with your branches. You can see which are remotes and which are local. This is a good headsup before you start to break things! It’s worth remembering that git branches are basically just a pointer, so to be able to work with those commits you need a local branch which points to somewhere from which those commits are reachable.
Thanks to Ben for the extra stuff, clarifications and calling me an idiot when I get git wrong, because I am, as it’s really pretty simple, except for the simple.












Thursday, October 25, 2012

return str(asdf) or unicode(asdf) ?




*** Python 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)] on win32. ***
*** Remote Python engine  is active ***
>>> import bleach
>>> print bleach.linkify('an http://example.com url')
an <a href="http://example.com" rel="nofollow">http://example.com</a> url
>>> print bleach.linkify('<a href="http://example.com" rel="nofollow">http://example.com</a>')
<a href="http://example.com" rel="nofollow">http://example.com</a>
>>> print bleach.linkify('an <a href="http://example.com" rel="nofollow">http://example.com</a> url')
an <a href="http://example.com" rel="nofollow">http://example.com</a> url
>>> print str(u'asdf')
asdf
>>> print str(u'asdf')
asdf
>>> print str('中国')
中国
>>> print str(u'中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> print unicode('中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
>>> print unicode('中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
>>> print str(u'中国')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> print str('asdf')
asdf
>>> print unicode('asdf')
asdf



>>> a = str('asdf')
>>> print a
asdf
>>> a
'asdf'
>>> b = unicode('asdf')
>>> print b
asdf
>>> b
u'asdf'



>>> print str('中国')
中国