Quick Edits Using VS Code

I’ve been working in Visual Studio pretty heavily in the last two weeks, but every once in a while I need to make quick edits to my .gitignore file, which isn’t in my project directory.

I usually open up a small text editor right from PowerShell and now that VS Code is out I thought ‘why not use that?’

Here’s how you can easily open files using code from PowerShell in three steps:

1.Find the path to VS Code
2. Edit your PowerShell profile
3. Open Files!

1. Find the VS Code Path

First thing we need to do is find where VS Code is in our directory.

If you have Code pinned to your start menu or on your desktop simply right click the icon and ‘select open file location’.

File explorer should now open to the location of the .exe.
Right click the Code.exe file and select ‘properties’.

If you selected a Shortcut Icon you should see a screen like this:

vscodeshortcutpath

If you navigated to the actual location in directory of VS Code it should look like this:

vscodelocationpath

Now right click and copy the path.

In my case: C:\Users\tireilly\AppData\Local\Code\app-0.1.0

2. Edit our PowerShell profile

To edit our profile we need to find the Microsoft.PowerShell_profile.ps1 file.

My file is located here:

profilelocationpowershell

I open the file in notepad to make my edits:

notepadpowershellprofile

Now that we have our profile open we’ll create an alias for labeled code followed by the path to our .exe
eg: Set-Alias code ‘C:\Users\user\AppData\Local\Code\app-0.1.0\Code.exe

noticed how I added the Code.exe to the path so the program will launch!

Here’s a photo of my current PowerShell profile for reference:

powershellprofile

Now we can save and close this file and open a new PowerShell window!

3. Edit some files!

Let’s edit our PowerShell profile with Code this time!

symmetryisgood

Something oddly satisfying about getting exactly what you want with words.

codeeditorpowershellprofile

And there we go. The brand new Code editor at your fingertips whenever you need it!

Let me know if you have any comment or questions!

Amazing Dude
Professor Chang Yun is an excellent man with an amazing imagine cup record. His mentorship has led teams to US finals for 8 years straight. With 6 teams making it into the World Finals.

GitHub’s Gitignore and Keeping DB Keys Safe

I’m working with a raspberry pi and I’m learning how to collect and manage sensor data.

There is a lot of data and that’s awesome, but dealing with remote persistent storage has helped me understand some good ways of keeping stuff organized and safe.

For example: ‘Keys’ or ‘Tokens’ (Not to be confused with the Late English writer Tolkien)

These are the keys to the door of your data.
To access the contents behind these doors, you must establish that you are the key-holder. One of the ways to do this in software is to create a long complicated string that can passed to the database so you can input or output data.

As long as you keep this token secret nobody will be able to mess with your data, or use it without having to pay for it.

Now this sounds pretty simple, but what if you use that token in code that is stored in a public repository on GitHub? This is essentially like printing a bunch of keys to your house and leaving them all over town. Not safe or smart.

So…

1. We need to keep these keys separate from the rest of your code.
2. We need to keep the file that contains those keys away from your public repository.

To do this I created a separate file called tokens.py that contains the strings of my tokens and a function called getStorageKey() that returns those strings.
Cool, we’ve satisfied our first goal.

Now we need to keep this file away from our repository. To do this we create a .gitignore file. This is a special ‘git’ file that allows us to specify which files will not be added to the remote repository.
To create a .gitignore file simply enter:

touch .gitignore

touch is a Unix command to create a file and update the access data, but not make any edits. Its the same as opening and closing without saving any changes.

After you’ve created this file you can edit it with any editor you like. I do it with sublime or VS Code because it helps me keep track of the separation occurring between visual studio and GitHub.

All you need to do is add on a separate lines the files you don’t want GitHub or just git to include.

When I first started all my .gitignore file had was:

tokens.py

Its expanded to include…

tokens.py
*.pyc

# the asterisk* acts as a wild card and will match any files with .pyc at the end
# anything that starts with a # is a comment and will be ignored from .gitignore

I saved this file (.gitignore) then added and committed it to the repository.

Now any files I add that end in .pyc or match tokens.py will not be added to my repository. And you will not be prompted to add them when you check git status.

What about files we’ve already have added???

If we add the rule after we added the file we want to ignore… We need to remove it from tracking on GitHub using the rm –cache command.

This will not remove it from your computer, but it will stop if from being tracked by GitHub, and will essentially be treated as if it was removed from your repository.

To pull this off simply type this command

git rm --cached filename.py

In this case I’m removing a file named ‘filename.py’.
After checking our git status we’ll see that this file is going to be removed.

Commit the changes and push to your remote repo.

You’ll see the files are still in your local directory, but are no longer in the remote or local repository. Hooray for keeping things safe!

Please let me know if you have any questions or comments.
The documentation is excellent and I recommend you spend some time reading through the resources below to round out your learning:

touch (Unix)
GitHub .gitignore
If you enter: ‘git ignore –help’ in your gitshell you can find more helpful documentation about using .gitignore
Setting up Code for Powershell
Setting up sublime for Powershell

SomeOfTheNorCalTeam
Evangelists in their natural habitat. Behind a booth and with a computer.

Running Multiple Python Versions on Windows

So, you are learning to develop with Python and you keep hopping back and forth from Python 2.x and Python 3.x and possibly versions in between.

Beside running everything in a virtual environment, its sometimes nice to just get to the different REPLs to test tiny pieces of code.

This can be managed easily in powershell using powershell profile.
This link helped me learn about what it is and how to set it up.

But basically, you establish shortcuts by customizing a profile page with the things you need to access quickly.

Currently in my Profile I have a shortcut for starting Python27, Python34, and Sublime.

I’ll show you how I set it up.
First we’ll edit our Profile, then we’ll change the execution policy to allow this file to edit PowerShell.

Open powershell!

1. Type the following command and press ENTER:

Test-Path $profile

2. If False you need to create a profile, if true go to step 3:

New-item -type file -force $profile

3. Go to you PowerShell Directory. Step 2 will show the path to the location. For me its Users\tireilly\Documents\WindowsPowerShell.

4. Open your profile:

Notepad .\Microsoft.PowerShell_profile.ps1

5. Now in Notepad you can add your aliases.

This is what I have:

Set-Alias subl 'C:\Program Files (x86)\Sublime Text 2\sublime_text.exe'
Set-Alias python34 'C:\Python34\python.exe'
Set-Alias python27 'C:\Python27\python.exe'

Make sure you have the proper path to your existence of python. I have mine in my Root directory.

Now to activate this you need to set the execution policy to allow this to be activate each time you open powershell.

To do this follow these steps:

1. Determine your current execution policy.

Get-ExecutionPolicy

This is just to double check you are restricted either way set it again to be sure.

2. Set Execution policy for yourself.

Set-ExecutionPolicy -Scope CurrentUser

3. You’ll be prompted with “ExecutionPolicy: ”
Set it as unrestricted
It should look like this:
ExecutionPolicy: Unrestricted"
Press enter.

4. Comfirm by entering ‘Y’ and pressing enter again.

Now Restart Power shell and try typing in your alias!
Does Python Start?

If not, I’m happy to help if you have any questions.

This Photo is from 2010... Just saying
This Photo is from 2010…