Tuesday, December 03, 2019

Pycharm IDE. How to fix missing source directories in Project view. Doesn't show file or folder structure.

Possibly due to a git merge between two of my computers on the same GIT project I lost the project structure. <
I was able to research this and re-add the content root under Preferences > Project > Project Structure. I wasn't as intuitive as I initially thought but here is the dialogue where I add it.
All fixed!

Tuesday, June 18, 2019

Being more pythonic with the shortcut conditional : A if C else B

I had these two blocks of "if else" code that I found too crude.

test_case_data = None
test_case_data2 = None

if test_already_exists(tests_object_list, test1_name) is not None:
  print(">>>> Test 1 already exists. skipping the add")
  print(">>>> Test 1 doesn't exist. adding")
  test_case_data = add_test_case(api_conn, int(project_id), test1_name)

if test_already_exists(tests_object_list, test2_name) is not None:
  print(">>>> Test 2 already exists. skipping the add")
  print(">>>> Test 2 doesn't exist. adding")
  test_case_data2 = add_test_case(api_conn, int(project_id), test2_name)
I took the code and applied Guido's "ternary solution," Link here.
All of the above code became three-four lines of code and I was able to apply a one-line Pythonic truthy and falsy evaluation on the return object. Very lambda-ish.
[Correction: What I had earlier with data = func_call if True else other_func_call, was incorrect. Corrected below. So it's really A if A (when A is not None) else B (which in this case creates a new test case object.]
for current_test in tests_lists:
  test_exists_data = test_already_exists() # Note returns None or or existing test data
  test_case_data = test_exists_data if test_exists_data else add_test_case()

  ...further code that doesn't contribute to A if C else B
As a note, this same elegance in code I found with the use of the "with," statement using the Context Manager. But for this, a lot more compact, readable and Pythonic.

Tuesday, May 21, 2019

When the default pip install won't work. Using pip directly with a github repo

I work at a company where we cannot publish some (or all) Python modules to pyPI.

95% of the time before last week I would use:

pip/pip3 install [module name]

to install modules that I needed.

If that doesn't work and you have access to the git repo, another command that might be successful is installing directly from the repo. Here's an example:

pip3 install git+https://github.com/[username]/repo

And if you've downloaded the repo using git clone and there's a setup.py, which should be standard for a properly maintained module - you can run:

pip3 install -e.

Remember, sometimes you need to work around less-standard, less user-friendly modules so thankfully there are other pip options that can hopefully serve your needs.

Wednesday, May 09, 2018

Trying out my Python 3 skills on HackerRank

I've been using HackerRank to get my Python coding chops up a little better then where they've been before.

Tried a medium challenge on HackerRank called Piling Up

My solution isn't as elegant as others but it works and constantly reduces the size of the cube size list that need to be rearranged from horizontal to vertical where the block size needs to be equal or smaller than the block below it.

for tests in range(int(input())):
    cube_length = int(input())
    list_of_cubes = list(map(int, input().split()))

    last_value_placed_on_stack = 0
    all_items_on_placed_on_stack = False
    cannot_stack_anymore = False

    a = 0
    z = int(len(list_of_cubes)) - 1

    while a < int(len(list_of_cubes)):
        if cannot_stack_anymore:
        if int(len(list_of_cubes)) == 1:
            if int(list_of_cubes[a]) <= last_value_placed_on_stack:
                all_items_on_placed_on_stack = True
        while z > 0:
            if list_of_cubes[a] >= int(list_of_cubes[z]):
                if list_of_cubes[a] <= last_value_placed_on_stack or last_value_placed_on_stack == 0:
                    last_value_placed_on_stack = list_of_cubes[a]
                    z = z - 1
                    cannot_stack_anymore = True
            if list_of_cubes[z] >= int(list_of_cubes[a]):
                if list_of_cubes[z] <= last_value_placed_on_stack or last_value_placed_on_stack == 0:
                    last_value_placed_on_stack = list_of_cubes[z]
                    z = z - 1   
                    cannot_stack_anymore = True

    if all_items_on_placed_on_stack:

Friday, April 27, 2018

Computing Fibonacci in Go

Had a coding whiteboard one of my first interviews back in early April.

It's nice to use Go Playground to prove it out and validate it after you get home.

For the whiteboard I had to do it iteratively. I added the recursive func after the fact

Here's the Go Playground link: https://play.golang.org/p/KDQaQlq8ONl

package main

import (

func main() {
fmt.Println("Hello, playground. Test for computing Fibonacci numbers")

computeFibonacciRecursively(10, 0, 1)

func computeFibonacci(numberOfNumbers int) {

penultimate := 0
ultimate := 1

fmt.Printf("\nNext number (iterative): %d", penultimate)
fmt.Printf("\nNext number (iterative): %d", ultimate)

for i := 2; i < numberOfNumbers; i++ {

currentSum := penultimate + ultimate
fmt.Printf("\nNext number (iterative): %d", currentSum)
penultimate = ultimate
ultimate = currentSum



func computeFibonacciRecursively(numberOfNumbers int, penultimate int, ultimate int) {

if numberOfNumbers == 0 {
} else {
fmt.Printf("\nNext number (recurse method): %d", penultimate)
computeFibonacciRecursively(numberOfNumbers, ultimate, penultimate+ultimate)



Friday, February 03, 2017

Powershell problem : With remote PSSession, Running EXE in path with spaces and getting back response using Write-Output

I think some of the most trouble I've recently had in coding Powershell has been trying to run an executable on a remote Hyper-V VM that was located under Program Files and then actually getting the response back properly so I could iterate though the results.

The first trickiness was with running the command properly when there were spaces in the path.

In double quotes you have to use the ampersand (&) and then write the path to the EXE in single quotes and then at the end of the string before the closing quotes character, append an argument.

For example :

$commandLocation = "& 'C:\Program Files\....\application_name.exe' argument"

This is the proper syntax that you can then pass to an Invoke-Expression that's inside a ScriptBlock called by Invoke-Command. Invoke-Expression is a very helpful commandlet to run EXEs, when, for example, running Powershell on a remote machine.

The second issue was understanding how I could use Write-Output to pipe the remote call output back a Powershell variable within the ScriptBlock that I could then use in the calling function.

I read that you could use 4>&1 at the end of the ScriptBlock but never found it useful in my case.

In my case, I just had a ScriptBlock assigned to a variable and then ran the ScriptBlock using an Invoke-Command on a separate line.

There was some funkiness with WriteHost appending to the invoke-command so you have to disable that and then return the pure unadulterated Invoke-Command Result that I assigned to a variable.

Then I could iterate over the lines in the method that called the method below.

Here's the example code :

Function RunCommandOnRemoteMachine([string] $blahParameter, [string] $remoteMachineAddress)

    $defaultBlahCommandLocation = "& 'C:\Program Files\...\blah.exe' "

    WriteLogAndConsole "Default blah directory : $defaultBlahCommandLocation"
    $fullCommand = $defaultBlahCommandLocation +  $blahParameter

    WriteLogAndConsole "Full command with arguments being run : $fullCommand"

    $scriptToExecute = { param($passedCommand)
        begin {
            Write-Host "Start of command:"
        process {
            $consoleOutput = Invoke-Expression -Verbose -Command $passedCommand
        end {
            Write-Host "Output from remote command:"
            Write-Host $consoleOutput
            Write-Host "************"
            Write-Output $consoleOutput

    $blahCommandResult = Invoke-Command -Session $global:session -Verbose -ScriptBlock $scriptToExecute -ArgumentList $fullCommand

    #Write-Host "Result of blah argument command: " $blahCommandResult

    return $blahCommandResult


Thursday, January 19, 2017

Powershell New-PS Drive using Administrator credentials. User name or password is incorrect

Sometimes you forget the obvious when your doing scripting.

Although not recommended, I was using powershell to create a new psdrive session using the Administrator.

Problem is that with just Administrator and the password, powershell was complaining about a bad username and password combination.

Since the VM was connected to a development domain I wasn't qualifying the Administrator name therefore confusing powershell.

I add the computername\Administrator and tried again. Much better success

Pycharm IDE. How to fix missing source directories in Project view. Doesn't show file or folder structure.

Possibly due to a git merge between two of my computers on the same GIT project I lost the project structure. < I was able to researc...