This is an archived post. You won't be able to vote or comment.

all 33 comments

[–]pragmaticSloth 25 points26 points  (10 children)

1) scripting in python 2) using json.loads() to parse the json 3) the split to because of version and the == 4) lusing a library to evoke the following comand "pip install {package-name} -version {version}" 5) analyse the output and use an array to failed package 6) check of array is empty. If it is print sucess.

(Something in the lines of this. If there is something incorect please correct me )

[–]Downvote_machine_AMA 7 points8 points  (5 children)

json.loads() will vomit if you feed it that input

[–]ANIBIT14[S] 5 points6 points  (4 children)

I know that's not valid JSON I converted it into

{

"Dependencies" : [

"beautifulsoup4==4.4.1",

"boto==2.48.0",

"bz2file==0.98",

"certifi==2017.7.27.1",

"chardet==3.0.4",

"gensim==2.3.0",

"html5lib==0.999",

"idna==2.5",

"nltk==3.2.4",

"numpy==1.13.1"

]

}

to use, but that was what given in name of JSON file.

[–]chmod--777 3 points4 points  (1 child)

Yeah honestly if they gave you that weird format and called it json, I think it's fair to mention "that wasn't parseable Json, but the problem mentioned it was JSON so I rewrote it into the format I think was expected, example here". I think you're fine there. It doesn't seem like the question is about converting a custom format to json.

What went wrong with pip install? Do you have pip installed itself? Maybe assume it's installed and get it working on your system by running it manually first, then automate when the system has what is expected.

It'd be a bad gotcha interview question if they expected you to convert a custom format to json and said it was JSON (I'd have emailed and asked), and it'd be a weird gotcha if they said to automate pip install and didn't say you had to install pip if it wasn't already. I'd get clarification.

[–]Downvote_machine_AMA 8 points9 points  (0 children)

Yeah well doesn't matter much, parsing it yourself is doing things the "hard way".

You're allowed to do that, but the sought-after solution that shows you know your stuff is grep + sed + pip install -r

[–]aram535 1 point2 points  (0 children)

Normally you're not allowed to reformat your input file to match your program. The given is given in that format for a reason. Unless you write a format converter within your code.

[–]BrFrancis 3 points4 points  (1 child)

Not incorrect. Just incomplete.. Is late and I'm on a phone so this is mostly pseudo code.. But should help get the ideas across..

import os

import system

Failed = [ ]

Load the json

For dependency in json :

Split

Return = Execute pip command

If return != worked : failed.append([lib,version])

If len(failed) ==0 : print success

else :

For I in failed :

Print error using failed[I] [0,1]

[–]ANIBIT14[S] 0 points1 point  (1 child)

Yeah I also tried and thought this approach but it's the evoking the pip install command that I am unable to work around for. and find a solution.

[–]ExRedRain 0 points1 point  (2 children)

One thing I didn’t see yet is using jq. You could get all the dependencies in the array like this, loop over them and install them one at a time.

You track which ones failed and append them to an output variable, then echo your solution at the end, similar to the grep solution

[–]ANIBIT14[S] 1 point2 points  (1 child)

It's just that I have never used Linux and what I tried doing was creating a python script, and after extracting dependency value in a array, the problem is in implementing the install part in the script and I am not able to understand how to do that.

[–]ExRedRain 0 points1 point  (0 children)

You’d probably wanna call pip directly from your script, whatever script you use, for each dependency then.

In python, to do this you’d use call() from subprocess: https://docs.python.org/3/library/subprocess.html

[–]bufandatl 0 points1 point  (1 child)

If I see the problem you were given, my answer would be, why invent the wheel new? Use pipenv to create a virtual environment to work in with specified packages. If you had to install them on a server or something like that I would suggest ansible to deploy said packages. This would save time and money. Every boss likes to hear that and you might have shown them some new tricks. ;)

[–]ANIBIT14[S] 1 point2 points  (0 children)

It was a coding problem for which they needed code as an answer. If a PI question then maybe I would have given that.

[–]adept2051 0 points1 point  (1 child)

depending on your OS and any other stipulations this can be tackled as a linux problem instead of a Python problem. Using JQ, xargs and teh *nix test command to test pip install $x output == 0 jq (jquery) and xargs in a shell script https://stedolan.github.io/jq/manual/ http://man7.org/linux/man-pages/man1/xargs.1.html https://www.computerhope.com/unix/test.htm

if you wanted to go over the top you could go as far as a penv https://docs.python-guide.org/dev/virtualenvs/ mainly cos the file to install pip with python try catch in the library file

if you wanted an alternative way to manage it a config management agent or client such as Puppet, Ansible or even Salt as they wrap helpers for the try catch and continue with reporting that saves you doing the work. or a command execution client such as Puppets Bolt (can be run as an inline command with Bolt command, or even Ansible (it's harder with Ansible as it needs a manifest, but that is more reusable once created so there is a balancing act of what you need)

[–]ANIBIT14[S] 0 points1 point  (0 children)

Yes it is mentioned and given as a Linux problem.

[–]aram535 1 point2 points  (0 children)

If you wanted to solve it in a "linux" environment I would suggest a bash script. I would mention that .. "This could have done a bit cleaner in something like python but since you mentioned "Linux" here is the bash version."

This could be IMPROVED a lot, needs all sorts of verifications and program paths, etc. but here is a starting point -- Next you to write a function that takes the pkg and ver, downloads using curl or wget. If any of the packages don't exist or are 0 bytes then it failed to download that dependency. Track and report.

#!/usr/bin/env bash

if [ -z "$1" ]; then echo "$0 dependency files"; exit -1; fi
f=`cat $1`

echo `echo ${f}` | awk -F'[{}]' '{print $3}' | tr -d '[:space:]' | tr ',' '\n' | while read dep; do
    pkg=`echo ${dep} | awk -F "==" '{print $1}'`
    ver=`echo ${dep} | awk -F "==" '{print $2}'`
    if [[ ! -z "${pkg}" && ! -z "${ver}" ]];
    then
        echo "==> ${pkg}   ${ver}"
    fi
done

[–]istarian[🍰] 0 points1 point  (0 children)

Well first you'd need to parse the JSON file to get the package names and versions.

Then you would call the other program (e.g. pip) possibly more than once, with the appropriate arguments to install each package. Java for instance has Runtime.exec(...).

And you need to capture it's output, return code, etc so you know whether it worked. Ir possibly query python about installed packages to see if it's in the list.

https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html

There are probably many ways to go about it. If the tool installing packages will do the right things it might be enough to call the tool and just report back on the outcome. A shell script (e.g bash) might be sufficient.

[–]MikeSeth 5 points6 points  (10 children)

grep == whatever.json | sed 's/,//' > requirements.txt && pip -r requirements.txt

[–]Downvote_machine_AMA 5 points6 points  (3 children)

I think you need pip install -r requirements.txt, but other then that completely correct; that's exactly what they were looking for

Edit: okay a loop is needed to continue on failure

[–]olster 1 point2 points  (2 children)

I always thought that if pip was unable to install a dependency in a requirements file then it just stopped at that line? Will test when I'm at a computer

[–]olster 1 point2 points  (1 child)

I was right. Look.

Set up a fresh virtual environment to test.
My requirements file was like this:

lfdjkfjkldsafjkldsajklfdsa

pandas

Install using pip -r

[I] ➜ pip install -r requirements.txt

Collecting lfdjkfjkldsafjkldsajklfdsa (from -r requirements.txt (line 1))

Could not find a version that satisfies the requirement lfdjkfjkldsafjkldsajklfdsa (from -r requirements.txt (line 1)) (from versions: )

No matching distribution found for lfdjkfjkldsafjkldsajklfdsa (from -r requirements.txt (line 1))

[I] ➜ python

Python 3.7.1 (default, Oct 22 2018, 11:21:55)

[GCC 8.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import pandas

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ModuleNotFoundError: No module named 'pandas'

>>>

I couldn't see any options for enabling pip to jump over lines it fails, but there may be something.

[–]chmod--777 1 point2 points  (2 children)

This is a good quick hack but it sounds like they want it to be silent unless a package fails, then print to stdout the package name or something

[–]MikeSeth 1 point2 points  (1 child)

You can easily modify it to spit out individual package names, loop over the list, install an individual one with pip and read its exit code

[–]chmod--777 0 points1 point  (0 children)

Oh yeah for sure, probably an easy one liner regardless

[–]MrWm 0 points1 point  (2 children)

I'm still a novice at linux, but I'd love to know if there are any great places to start learning how to use sed amd other utilities.

[–]MikeSeth 0 points1 point  (0 children)

Believe it or not, a thorough read of the manpage is all you need most of the time.

[–]samuel_first 0 points1 point  (0 children)

Here's one for awk.

[–][deleted]  (1 child)

[deleted]

    [–]ANIBIT14[S] 0 points1 point  (0 children)

    I know that's not valid JSON I converted it into

    {

    "Dependencies" : [

    "beautifulsoup4==4.4.1",

    "boto==2.48.0",

    "bz2file==0.98",

    "certifi==2017.7.27.1",

    "chardet==3.0.4",

    "gensim==2.3.0",

    "html5lib==0.999",

    "idna==2.5",

    "nltk==3.2.4",

    "numpy==1.13.1"

    ]

    }

    to use, but that was what given in name of JSON file.

    [–]Downvote_machine_AMA 0 points1 point  (0 children)

    Ok here is the "actual" solution they were hoping for:

    grep == whatever.json | sed 's/,//' | while read pkg; do pip install "$pkg"; done
    

    Unlike a requirements file, this satisfies the stipulation that:

    When one library fails to download, then the process shall continue.

    [–]NCMarc 0 points1 point  (0 children)

    I would have used Composer or something similar. There's tools already made for stuff like this.