Overview
The Numbered Display in Vim (or Ruler) is extremely helpful when programming. However, sometimes, such as when you need to copy text from vim and paste to another program, you need to turn off the numbered display.
Solution
To do this, type this in vim
:set nonumber
The numbers should disappear. If you want them to reappear, type this in vim
:set number
NOTE: Please note that this guide is currently in rough draft form. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Monday, March 10, 2014
Python Style: Why is it So Important to Follow Conventions?
Overview
Conventions are in there for a reason. They make things easy to scale over time by providing easy-to remember guidelines for people to follow. In python, you don't have to follow most of the guidelines. However, you should try to follow all of the guidelines when possible. It makes it easier for people to read your code as well as helps yourself search through your code faster and remember where things are.
Readability
Ever read something that had horrendous grammar? Maybe a post on my site? Then you know how hard it is to read something that doesn't follow grammer. When you don't follow conventions, you are basically writing code without following python's grammar. Sure, you can write something without grammar, but you and other people won't be able to read it/maintain it later. Writing code that is maintainable is one of the most important things in programming, because even if your program's functionality should never change, the system its running on certainly will (Like the change from python2.7 to python 3.2).
Searching Your Code
Ever have to grep in your terminal in order to find a method or variable in your program? By following conventions, you can find things faster and easier. Say you're trying to find an instance variable. If you did it right, you'll search "_var_name". This will help you weed out all of the things that have "var_name" in them, like method names. Say you want to find a method that starts with "foo". All you have to search is "def foo" and you'll be taking to all the methods that start with "foo".
Remembering Where Things Are
By Following conventions, you can remember where things are put in your program. Say you have a class file with a bunch of methods, helper methods, variables and other things. If you scatter all of these across the file, it will be harder for you and others to find methods or variables of interest to you/them. If you instead put all of the variables together, then helper methods together, then regular methods together, you'll be able to find things a lot quicker later.
Conclusion
Study and Follow conventions. You'll be able to read your code and other code faster with more comprehension.
NOTE: Please note that I am not an expert at this topic and this info can certainly be improved. If you have questions, comments or suggestions for this blog post, please comment! Also, this guide is currently in rough draft form. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Conventions are in there for a reason. They make things easy to scale over time by providing easy-to remember guidelines for people to follow. In python, you don't have to follow most of the guidelines. However, you should try to follow all of the guidelines when possible. It makes it easier for people to read your code as well as helps yourself search through your code faster and remember where things are.
Readability
Ever read something that had horrendous grammar? Maybe a post on my site? Then you know how hard it is to read something that doesn't follow grammer. When you don't follow conventions, you are basically writing code without following python's grammar. Sure, you can write something without grammar, but you and other people won't be able to read it/maintain it later. Writing code that is maintainable is one of the most important things in programming, because even if your program's functionality should never change, the system its running on certainly will (Like the change from python2.7 to python 3.2).
Searching Your Code
Ever have to grep in your terminal in order to find a method or variable in your program? By following conventions, you can find things faster and easier. Say you're trying to find an instance variable. If you did it right, you'll search "_var_name". This will help you weed out all of the things that have "var_name" in them, like method names. Say you want to find a method that starts with "foo". All you have to search is "def foo" and you'll be taking to all the methods that start with "foo".
Remembering Where Things Are
By Following conventions, you can remember where things are put in your program. Say you have a class file with a bunch of methods, helper methods, variables and other things. If you scatter all of these across the file, it will be harder for you and others to find methods or variables of interest to you/them. If you instead put all of the variables together, then helper methods together, then regular methods together, you'll be able to find things a lot quicker later.
Conclusion
Study and Follow conventions. You'll be able to read your code and other code faster with more comprehension.
NOTE: Please note that I am not an expert at this topic and this info can certainly be improved. If you have questions, comments or suggestions for this blog post, please comment! Also, this guide is currently in rough draft form. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Python Style: Refrain from Setting/Getting Instance Variables Outside your Class
Overview
Instance Variables can cause some really bad stuff to happen in your program, but they're basically necessary to have in any class to keep track of the state of an object. Even worse is that there are no private variables in python. Because of this, it's especially important to make sure your programs have setters and getters, as well as naming your variables in a way that prevents other classes/programs from accessing them directly.
What Are Getters?
Getters are just methods in your class that return the variable, or variation of the variable, that the user wants. They allow you to send the variable to whoever asks for the variable on your terms. For example, lets say a person asks for data of a user. In your database, you've stored the user object with a password that you don't want others to see. So, when someone calls your method, you return them all of the info except the password.
Getters also allow you to keep track of anyone who asks for the variable. Lets say you want to find all of the referred methods that call your getter. You can set a console.log to print out the stack trace every time the method is called. This would allow you to go through your log files later and see where your getter was called from.
What Are Setters?
Setters are very similar to Getters, but instead are used to set the variables in your class. They are very useful for making sure variables are set correctly, providing extra info into your variables, and seeing who's set your variables.
Setters make sure variables are set correctly because you're able to add extra checking to make sure the variables don't become corrupt. For example, lets say you have a bank app that stores dict objects. These dict objects look like this: {'deposit': 500}. Lets say a user decided to just add a dict object like this: {'deposit': 'foo'}. 'foo' is not a number, so the program is likely to crash. If you have a setter, you can check the key and value of the dict and make sure each are valid when being set, and, if they aren't, send a nicely-worded error message back to the user.
Setters can also provide extra, very useful data into your variables. Lets say you want to add a deposit, and you want to also record when that deposit was made. Well, you don't really want to trust the user to pass in the proper timestamp, because he can send a made up timestamp. So, in your method you record the time the method was called in the variable, so the variable would look like this: {'deposit': 500, 'time_created': 523423423}
Setters allow you to see who has set your variables. Like in the getters example, you can set a console.log to log a stacktrace to see everywhere your method was called.
Debugging Reasons
One of my favorite reasons is for Debugging. If someone sets the variable directly, its very hard to track down where it happened. You can grep all your files, but if it came from some other source, then you are not going to have much luck. If you have setters and getters, you can set a pdb.set_trace() in those methods and see every time you get the value and you set the value.
Creating your Instance Variables Correctly
Anyone can change any variable in your class, but most people don't. If you follow the PEP8 Guide, you can be sure that the fewest number of programmers try to change your instance variables. To do this, add an underscore before your variable name, like this: _var_name. This is the syntax to say that variable is "private". If you want to make it really hard on programmers trying to screw things up, put 2 underscores before your variable name, like this: __var_name. This obfuscates the variable, making it harder for people to change it.
Conclusion
Setters and Getters are always a good idea. Use them.
References:
Instance Variables can cause some really bad stuff to happen in your program, but they're basically necessary to have in any class to keep track of the state of an object. Even worse is that there are no private variables in python. Because of this, it's especially important to make sure your programs have setters and getters, as well as naming your variables in a way that prevents other classes/programs from accessing them directly.
What Are Getters?
Getters are just methods in your class that return the variable, or variation of the variable, that the user wants. They allow you to send the variable to whoever asks for the variable on your terms. For example, lets say a person asks for data of a user. In your database, you've stored the user object with a password that you don't want others to see. So, when someone calls your method, you return them all of the info except the password.
Getters also allow you to keep track of anyone who asks for the variable. Lets say you want to find all of the referred methods that call your getter. You can set a console.log to print out the stack trace every time the method is called. This would allow you to go through your log files later and see where your getter was called from.
What Are Setters?
Setters are very similar to Getters, but instead are used to set the variables in your class. They are very useful for making sure variables are set correctly, providing extra info into your variables, and seeing who's set your variables.
Setters make sure variables are set correctly because you're able to add extra checking to make sure the variables don't become corrupt. For example, lets say you have a bank app that stores dict objects. These dict objects look like this: {'deposit': 500}. Lets say a user decided to just add a dict object like this: {'deposit': 'foo'}. 'foo' is not a number, so the program is likely to crash. If you have a setter, you can check the key and value of the dict and make sure each are valid when being set, and, if they aren't, send a nicely-worded error message back to the user.
Setters can also provide extra, very useful data into your variables. Lets say you want to add a deposit, and you want to also record when that deposit was made. Well, you don't really want to trust the user to pass in the proper timestamp, because he can send a made up timestamp. So, in your method you record the time the method was called in the variable, so the variable would look like this: {'deposit': 500, 'time_created': 523423423}
Setters allow you to see who has set your variables. Like in the getters example, you can set a console.log to log a stacktrace to see everywhere your method was called.
Debugging Reasons
One of my favorite reasons is for Debugging. If someone sets the variable directly, its very hard to track down where it happened. You can grep all your files, but if it came from some other source, then you are not going to have much luck. If you have setters and getters, you can set a pdb.set_trace() in those methods and see every time you get the value and you set the value.
Creating your Instance Variables Correctly
Anyone can change any variable in your class, but most people don't. If you follow the PEP8 Guide, you can be sure that the fewest number of programmers try to change your instance variables. To do this, add an underscore before your variable name, like this: _var_name. This is the syntax to say that variable is "private". If you want to make it really hard on programmers trying to screw things up, put 2 underscores before your variable name, like this: __var_name. This obfuscates the variable, making it harder for people to change it.
Conclusion
Setters and Getters are always a good idea. Use them.
References:
- http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes
NOTE: Please note that I am not an expert at this topic and this info can certainly be improved. If you have questions, comments or suggestions for this blog post, please comment! Also, this guide is currently in rough draft form. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Ubuntu: How to Turn Off Your Trackpad
Overview
Trackpads can be very annoying when programming on laptops. Your palm can accidentally brush against it when pressing keys and click into another program.
Solution
In Ubuntu, you can easily disable the touch pad from the command line. Todo so, follow these steps:
Your touch pad should now be disabled. To re-enable, just change the ending 0 in step 4 to a 1 like this: xinput set-prop <id> "Device Enabled" 1
Conclusion
Turning off your touch pad will be very useful in situations where you only want to use your keyboard, like if you're programming. However, some things, like using a website, will still require you to use the touch pad. This is why I suggest creating a bash script to enable and disable the touch pad so you don't have to remember these commands every time.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and pictures. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Trackpads can be very annoying when programming on laptops. Your palm can accidentally brush against it when pressing keys and click into another program.
Solution
In Ubuntu, you can easily disable the touch pad from the command line. Todo so, follow these steps:
- Open up your terminal
- Execute this command: xinput
- This command will show you information about the inputs that are connected to the laptop.
- Search for "TouchPad" and find the "id" that is on that line (example: id=10)
- Disable the touch pad by executing this command (Replace <id> with whatever id you got from step 3): xinput set-prop <id> "Device Enabled" 0
- Example if your id is 10: xinput set-prop 10 "Device Enabled" 0
Your touch pad should now be disabled. To re-enable, just change the ending 0 in step 4 to a 1 like this: xinput set-prop <id> "Device Enabled" 1
Conclusion
Turning off your touch pad will be very useful in situations where you only want to use your keyboard, like if you're programming. However, some things, like using a website, will still require you to use the touch pad. This is why I suggest creating a bash script to enable and disable the touch pad so you don't have to remember these commands every time.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and pictures. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Sunday, March 9, 2014
Ubuntu: How to Force-Quit an Application in Ubuntu
Overview:
Something can go wrong on all computers. Ubuntu is no exception. One of the most common things to go wrong is a program has stalled and you can't seem to stop it.
How you Solve this on Mac/Windows:
Mac and Windows computers seem to make it easy on you, having nice, command-line free ways of quitting a program. On Mac you can just look at running programs and do "force-quit" on the application that has stopped running. On windows you can do "Cntrl-Alt-Delete" and quit the program that has stopped running.
How Ubuntu is Different:
Ubuntu doesn't seem to make it as easy on you, but this is how you do it:
1) Open the terminal
2) execute the command "xkill" (Without the quotations)
3) With your cursor (Also called mouse), click on the window you want to quit
Why Ubuntu is still better:
I use all 3 operating systems on a daily basis and while I regularly have to force quit programs on my mac/windows machines, I only have to do this once or twice a year with my ubuntu machines.
Conclusion:
It might be slightly more difficult in ubuntu mainly because the easiest way you need to open your terminal. If you're a programmer, this should be simple to do.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and pictures. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Something can go wrong on all computers. Ubuntu is no exception. One of the most common things to go wrong is a program has stalled and you can't seem to stop it.
How you Solve this on Mac/Windows:
Mac and Windows computers seem to make it easy on you, having nice, command-line free ways of quitting a program. On Mac you can just look at running programs and do "force-quit" on the application that has stopped running. On windows you can do "Cntrl-Alt-Delete" and quit the program that has stopped running.
How Ubuntu is Different:
Ubuntu doesn't seem to make it as easy on you, but this is how you do it:
1) Open the terminal
2) execute the command "xkill" (Without the quotations)
3) With your cursor (Also called mouse), click on the window you want to quit
Why Ubuntu is still better:
I use all 3 operating systems on a daily basis and while I regularly have to force quit programs on my mac/windows machines, I only have to do this once or twice a year with my ubuntu machines.
Conclusion:
It might be slightly more difficult in ubuntu mainly because the easiest way you need to open your terminal. If you're a programmer, this should be simple to do.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and pictures. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Sunday, March 2, 2014
Losing Weight: Why you Should get Good Sleep to Lose Weight
Overview
Losing weight is mostly about avoiding craving food and making sure you're eating less than you are burning.
Why you should avoid Craving
Craving, or wanting to eat something, leads to frustration and binge eating. This is why we want to avoid craving as much as possible to lose weight.
Why you should Eat Less than you Burn to lose weight
This is the only way to lose weight. If you don't want to lose weight, don't follow this rule.
Why you should get good sleep
When you don't get good sleep, you get tired more often. When you get tired, you start to crave food more often.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and references. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Losing weight is mostly about avoiding craving food and making sure you're eating less than you are burning.
Why you should avoid Craving
Craving, or wanting to eat something, leads to frustration and binge eating. This is why we want to avoid craving as much as possible to lose weight.
Why you should Eat Less than you Burn to lose weight
This is the only way to lose weight. If you don't want to lose weight, don't follow this rule.
Why you should get good sleep
When you don't get good sleep, you get tired more often. When you get tired, you start to crave food more often.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and references. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Losing Weight: Why you should eat a large breakfast
Overview
When I was younger, I never ate breakfast. I never had time for it as I would wake up 10 minutes before school started and would have to sprint to get to school that was 15 minutes away. However, this would leave me craving for food until lunch.
Problem
Craving food often leads to overeating. You tend to over-compensate when you are too low to make sure your cravings go away. However, because your body typically takes 20 minutes to realize when you've actually satisfied your hunger, it's not great to just eat until you feel "Full."
How to stop craving food
You want to avoid craving food as much as possible. In my experience, I never feel hungry in the morning, so my body feels I can skip it. However, to keep from craving food, you should put something in your stomach about every 2 hours. This makes sure your body knows you are feeding it and doesn't have to remind you.
Why you should eat more earlier
You want to give your body the longest amount of time to burn the stuff you eat. If you eat more at night, then you only have a few hours to burn off those extra calories. It allows you to not feel as tired later because your body knows it has energy to burn. It also reduces cravings later because your body isn't just burning the fat at first.
You may be saying "But I want to lose weight, so I should eat a lot less, and be skipping breakfast, I eat less." You are right in saying you lose more weight if you eat less, but that's only if you keep up your exercise routine. If you get frustrated at any point and decide to just quite, you aren't going to lose any weight. The key to losing weight is persistence in your routine. By eating more in the morning, you'll have that wood and kindling to start that fire in your belly everyday to keep going.
Conclusion
Eat more early. You'll be happier this way.
NOTE: This guide is currently in rough draft form and could be improved deeper explanation and more references. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
When I was younger, I never ate breakfast. I never had time for it as I would wake up 10 minutes before school started and would have to sprint to get to school that was 15 minutes away. However, this would leave me craving for food until lunch.
Problem
Craving food often leads to overeating. You tend to over-compensate when you are too low to make sure your cravings go away. However, because your body typically takes 20 minutes to realize when you've actually satisfied your hunger, it's not great to just eat until you feel "Full."
How to stop craving food
You want to avoid craving food as much as possible. In my experience, I never feel hungry in the morning, so my body feels I can skip it. However, to keep from craving food, you should put something in your stomach about every 2 hours. This makes sure your body knows you are feeding it and doesn't have to remind you.
Why you should eat more earlier
You want to give your body the longest amount of time to burn the stuff you eat. If you eat more at night, then you only have a few hours to burn off those extra calories. It allows you to not feel as tired later because your body knows it has energy to burn. It also reduces cravings later because your body isn't just burning the fat at first.
You may be saying "But I want to lose weight, so I should eat a lot less, and be skipping breakfast, I eat less." You are right in saying you lose more weight if you eat less, but that's only if you keep up your exercise routine. If you get frustrated at any point and decide to just quite, you aren't going to lose any weight. The key to losing weight is persistence in your routine. By eating more in the morning, you'll have that wood and kindling to start that fire in your belly everyday to keep going.
Conclusion
Eat more early. You'll be happier this way.
NOTE: This guide is currently in rough draft form and could be improved deeper explanation and more references. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).
Beginning to learn Electrical Engineering: Important Formulas (Part 1)
Overview
I'm someone who loves learning anything engineering, and something I've been lacking is an understanding of electrical engineering and how electronics actually work. So I think I'll start a blog entry series that will cover learning electrical engineering, from absolute beginner to at least competent.
Why learn electrical engineering concepts?
There is a bunch you can do with programming alone, but to make really cool physical projects, like running a toy car or building something that checks temperature, you'll need to program for things like Arduino or raspberry pi. These will require at least introductory knowledge on how circuits work and what resistors are in order to not destroy your electronics.
Basic Formulas
Key of Symbols:
E = energy in Joules
I = current in Amperes
t = time in seconds
V = voltage in volts
Q = Charge in Coulombs
R = resistance in Ohms
P = Power in Watts (Amount of energy transferred)
Energy (E)
E = ItV
Current (I)
I = P/V
I = dQ/dt
Voltage (Also called Potential Difference) (V)
V = IR
V = W/Q
Resistance (R)
R = V/I
Power (P)
P = IV
P = I^2R
P = V^2/R
Conclusion
This is just a basic overview of the beginning formulas for what will be talked about in later blog posts. You should study these equations and try to memorize them first in order to be proficient doing equations later.
Resources
These notes were derived mainly from: http://www.bmatcrashcourse.com/electricity-notes.pdf. I highly recommend looking over this document as it has the same information, but includes nice diagrams/pictures for reference.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and pictures. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about). This post deals with equations, and is very susceptible to errors. If you see an error, please leave a comment and I will fix it asap!
Resources
These notes were derived mainly from: http://www.bmatcrashcourse.com/electricity-notes.pdf. I highly recommend looking over this document as it has the same information, but includes nice diagrams/pictures for reference.
NOTE: This guide is currently in rough draft form and could be improved with clearer instructions and pictures. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about). This post deals with equations, and is very susceptible to errors. If you see an error, please leave a comment and I will fix it asap!
Subscribe to:
Posts
(
Atom
)