Some guy's stuff on what he wishes he learned (TODO: Paraphrase here.. or copy and paste in case it goes away..)
Formal education plan
How to get better at anything in 10,000 hours
Wednesday, June 26, 2013
Tuesday, June 25, 2013
Why use Python Properties?
The reasoning is here, but in short "...[t]he answer is, that in [a simple] use case we would not. In fact, we would
write thus:
'But!', I can hear you scream, 'there's no encapsulation!'. What will we do if we need to control access to x, make it read-only or do something else to it? Won't we have to refactor everything to the getters and setters that we avoided?
No - we just switch to the property version, add whatever we want, and have not changed the interface one iota! The great thing about properties is not that they replace getters and setters, its that you don't have to write them to future-proof your code. You can start out by writing the simplest implementation imaginable, and if you later need to change the implementation you can still do so without changing the interface. Neat, huh?"
>>> class MyClass(object): ... x = 0 ... >>> my = MyClass() >>> my.x = 4 >>> my.x 4
'But!', I can hear you scream, 'there's no encapsulation!'. What will we do if we need to control access to x, make it read-only or do something else to it? Won't we have to refactor everything to the getters and setters that we avoided?
No - we just switch to the property version, add whatever we want, and have not changed the interface one iota! The great thing about properties is not that they replace getters and setters, its that you don't have to write them to future-proof your code. You can start out by writing the simplest implementation imaginable, and if you later need to change the implementation you can still do so without changing the interface. Neat, huh?"
Wednesday, May 29, 2013
Python2's urllib2 is a miserable libary
Python2's urllib2 is a miserable piece of shit of a library. Use requests instead.
Labels:
python,
requests,
rumbling-ramblings,
urllib2
Friday, May 10, 2013
Install firefox 20 addons offline (via the command line)
So much misinformation out there, that it took a while to figure this out. And seeing that the documentation for mozilla firefox is an out-of-date mess, this will probably be out of date in a few versions as well.
Currently the only way I've gotten an add-on to install automatically is as follows:
Currently the only way I've gotten an add-on to install automatically is as follows:
- Download the add-on. You can simply go to the page for the add-on at https://addons.mozilla.org/en-US/firefox/ and right click on the 'Add to Firefox' button and select 'Save Link As...'
- Get the application id of the add-on and rename the .xpi file you downloaded to
.xpi. Typically the id is a uuid like {D4DD63FA-01E4-46a7-B6B1-EDAB7D6AD389}, but sometimes it's a string such as an email address or webaddress. NOTE that if you have an id of {ec8030f7-c20a-464f-9b0e-13a3a9e97384}, you have the WRONG id. That is the id for the firefox application itself (this value might change with different versions of firefox). - Move the renamed xpi file to either your local mozilla profile directory at ~/.mozilla/firefox/
-profile_id-/extensions/ or to the global directory at /usr/lib/firefox-addons/extensions/. The -profile_id- can be found in ~/.mozilla/firefox/profiles.ini. You'll have to find the correct Profile section and then the 'Path' for that profile (usually there's just one profile section, Profile0 with 'Name' = default). - Restart firefox. You'll be prompted to install the add-ons you've added to the extensions directory once firefox starts up.
Monday, April 29, 2013
Using the multi command in zookeeper to store objects larger than 1MB
Quoting the third answer to the questions asked here.
The idea would be that you would upload multiple pieces of the large object separately into different znodes (without using multi)
Then you would update a pointer node that has references to the pieces while controlling for the version of the pieces (using a multi).
Friday, January 25, 2013
Issues in python unittests using Python Mock
In running some unit tests, my assertions were returning things like
It does this because the object you are mocking doesn't have anything specified to handle the members that are being accessed on it. That is if you have
but you call
Mock will intercept this and create a new mock object with the name 'x.z'. You can string these along and get a name such as 'x.z.foo().bar()'
The fix to my original issue (the AssertionError) is to simply handle the attribute correctly
Edit: Added rest of mock object match the example better
AssertionError: Mock name='mock.RiakClient().bucket().get().get_data()' id='39021008' != 'test-data'
It does this because the object you are mocking doesn't have anything specified to handle the members that are being accessed on it. That is if you have
x = Mock()
x.y = 'thing'
but you call
x.z
Mock will intercept this and create a new mock object with the name 'x.z'. You can string these along and get a name such as 'x.z.foo().bar()'
The fix to my original issue (the AssertionError) is to simply handle the attribute correctly
x = Mock()mock_get_data = mock(return_value='test-data') #one-liner mock_get = Mock(get=Mock(return_value=mock_get_data)#more readable two-liner mock_bucket = Mock()mock_bucket.bucket = Mock(return_value=mock_get)
x.RiakClient.return_value = mock_bucketEdit: Added rest of mock object match the example better
Wednesday, December 12, 2012
Messing up with command-line arguments in Bash: $*, $@, "$*", "$@",…
Figured out a stupid issue that plagued a bit of code I had for quite a while. Didn't care about it enough to google it till recently tho.
Quoting the source:
Quoting the source:
It’s stupid, but it took me a good hour to figure this out, so maybe I’m not the only one…
I’ve recently had a problem with command-line arguments in my Java program. The problem was that command line arguments containing spaces were parsed incorrectly, i.e. chopped into individual arguments. My initial suspect was gnu.Getopt package I use for parsing arguments, but as it turned out I was wrong.
The real culprit was a shell wrapper script I used to wrap my java code. The code was the following:
BTW: There’s also
I’ve recently had a problem with command-line arguments in my Java program. The problem was that command line arguments containing spaces were parsed incorrectly, i.e. chopped into individual arguments. My initial suspect was gnu.Getopt package I use for parsing arguments, but as it turned out I was wrong.
The real culprit was a shell wrapper script I used to wrap my java code. The code was the following:
java -some parameters- -programm .class- $@
See the problem? I didn’t. You need quotes around "$@" in which case the parameter gets expanded to: "$1" "$2" "$3"... With no quotes the shell expands it to $1 $2 $3, hence all parameters containing spaces get chopped (also globbing takes place in this case).BTW: There’s also
"$*" which is used to combine all parameters into a single one, i.e, "$*" expands to "$1c$2c$3c..., where c is $IFS (or space). Here it’s also important to have it enclosed in quotes.
Subscribe to:
Posts (Atom)